The background color of the selected item in an unedited JComboBox

The background color of the selected item in an unedited JComboBox is kind of blue:

alt text

Is there a way to make this a different color like white?

+5
source share
3 answers

This should work

jComboBox1.setRenderer(new DefaultListCellRenderer() {
    @Override
    public void paint(Graphics g) {
        setBackground(Color.WHITE);
        setForeground(Color.BLACK);
        super.paint(g);
    }
});
+8
source

The background assigned by the renderer is overridden by the background color of the JList selection, which is used in the pop-up window for the combo box. Check out the paintCurrentValue method of the BasicComboBoxUI class. Therefore, a workaround:

JComboBox comboBox = new JComboBox(...);
Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup)child;
JList list = popup.getList();
list.setSelectionBackground(Color.RED);

. , , , .

+6

, , ListCellRenderer?

, :

 public Component getListCellRendererComponent(JList list,
                                               Object value,
                                               int index,
                                               boolean isSelected,
                                               boolean cellHasFocus) {
+3

All Articles