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

setBackground on editable JComboBox doesn't work

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      It's not possible to change the background color on editable JComboBoxes. After calling setBackground() on the JComboBox and its editor, the background color of the selected item (i.e. the content of the editor) remains white.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a JComboBox
      2. Call setBackground(Color.YELLOW) on the JComboBox
      3. Call setBackground(Color.YELLOW) on the editor of the JComboBox
      4. Show the JComboBox within a Frame

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The background of the selected item should be yellow.
      ACTUAL -
      The background remains white despite calling getBackground() on the JComboBox and its editor shows the correct value (yellow).

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.*;

      import javax.swing.*;
      import javax.swing.plaf.*;

      public class ComboBoxBackgroundTest extends JFrame {

      public ComboBoxBackgroundTest() {
      super("ComboBoxTest");

      Container contentPane = getContentPane();
      contentPane.setLayout(new GridBagLayout());

      final Color focusColor = Color.YELLOW;

      final JComboBox box1 = new JComboBox(new Object[]{"Entry 1", "Entry 2"});
      box1.setEditable(true);
      box1.setOpaque(true);
      box1.setBackground(focusColor);
      box1.getEditor().getEditorComponent().setBackground(focusColor);

      GridBagConstraints c = new GridBagConstraints();
      c.gridx = 0;
      c.gridy = 0;
      c.gridwidth = 1;
      c.gridheight = 1;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.anchor = GridBagConstraints.WEST;
      c.weightx = 1.0;
      c.weighty = 0.0;
      c.insets = new Insets(20,10,0,10);
      ((GridBagLayout)contentPane.getLayout()).setConstraints(box1, c);
      contentPane.add(box1);

      JButton print = new JButton("show background colors");
      print.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      System.out.println("Background JComboBox: " + box1.getBackground());
      System.out.println("Editor-Background ComboBoxEditor: "
      + box1.getEditor().getEditorComponent().getBackground());
      }
      });

      c = new GridBagConstraints();
      c.gridx = 0;
      c.gridy = 1;
      c.gridwidth = 1;
      c.gridheight = 1;
      c.fill = GridBagConstraints.NONE;
      c.anchor = GridBagConstraints.CENTER;
      c.weightx = 0.0;
      c.weighty = 0.0;
      c.insets = new Insets(20,10,20,10);
      ((GridBagLayout)contentPane.getLayout()).setConstraints(print, c);
      contentPane.add(print);
      }

      /**
      * @param args
      */
      public static void main(String[] args) {
              try {
      try {
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      } catch (Exception exception) {
      System.out.println("WARNUNG: Unsupported L&F !!!");
      }

                  JFrame frame = new ComboBoxBackgroundTest();
                  frame.setSize(new Dimension(500, 200));
                  frame.setVisible(true);

                  frame.addWindowListener(new WindowAdapter() {
                      @Override
      public void windowClosing(WindowEvent e) {
                          System.exit(0);
                      }
                  });
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
      }
      }
      ---------- END SOURCE ----------

            peterz Peter Zhelezniakov
            keshah Ketan Shah (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: