facebook

JComboBox renderer problem

  1. MyEclipse Archived
  2.  > 
  3. Matisse/Swing UI Development
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #304175 Reply

    pbassi
    Member

    Hi,

    can anybody help me understand why the combobox renderer below is setting the background color correctly, but not the foreground one?

    Thanks in advance.
    Paolo

    public class BUComboBox extends JComboBox {
    
        public BUComboBox(ComboBoxModel aModel) {
            super(aModel);
            ComboBoxRenderer renderer= new ComboBoxRenderer();
            renderer.setPreferredSize(new Dimension(200, 20));
            this.setRenderer(renderer);
        }
    
        private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
            public ComboBoxRenderer() {
                setOpaque(true);
            }
    
            public Component getListCellRendererComponent(
                    JList list,
                    Object value,
                    int index,
                    boolean isSelected,
                    boolean cellHasFocus) {
    
                if (isSelected) {
                    setBackground(Color.BLUE); 
                    setForeground(Color.WHITE); 
                } else {
                    setBackground(Color.WHITE); 
                    setForeground(Color.BLUE);
                }
                return this;
            }
        }
    }
    
    #304177 Reply

    pbassi
    Member

    Forget about the question, I found the problem, I was not setting the value in the renderer…

    Thanks!

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: JComboBox renderer problem

You must be logged in to post in the forum log in