Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6363287

JComboBox changes the background of it's renderer

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.5.0_05"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [verziószám: 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      On initialization, I set a JLabel's background color, to let's say red.
      If I use a custom cellRenderer for a JComboBox that returns this red JLabel, the JComboBox will change the label's background color, so that it will not display red

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect the JComboBox renderin mechanism to NOT change the Color of the JLabel I use
      ACTUAL -
      It changes the Color from red (in this case) to javax.swing.plaf.ColorUIResource[r=163,g=184,b=204]

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Color;
      import java.awt.Component;
      import javax.swing.JLabel;
      import javax.swing.JList;
      import javax.swing.ListCellRenderer;

      public class Test3 extends javax.swing.JFrame implements ListCellRenderer{
          JLabel renderer=new JLabel();
          
          public Test3() {
              initComponents();
              renderer.setBackground(Color.red);
              jComboBox1.setRenderer(this);
              jComboBox1.revalidate();
              setSize(100,70);
              setLocationRelativeTo(null);
          }
          
          public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus){
              //renderer.setBackground(Color.red);
              renderer.setText(value.toString());
              renderer.setOpaque(isSelected);
              renderer.setForeground(isSelected ? Color.white : Color.black);
              System.out.println("Background Color of JLabel: "+renderer.getBackground());
              return renderer;
          }
          
          private void initComponents() {
              jComboBox1 = new javax.swing.JComboBox();

              getContentPane().setLayout(null);

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
              jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3" }));
              getContentPane().add(jComboBox1);
              jComboBox1.setBounds(0, 0, 105, 22);

              pack();
          }
          
          public static void main(String args[]) {
              java.awt.EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      new Test3().setVisible(true);
                  }
              });
          }
          
          private javax.swing.JComboBox jComboBox1;
          
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      You need to set the Color of the JLabel each time getCellRendererComponent is called

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: