Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background color of the selected item in an uneditable JComboBox

The background color of the selected item in an uneditable JComboBox is a sort of blue:

alt text

Is there any way to make this a different color, such as white, for example?

like image 748
Paul Reiners Avatar asked Dec 10 '10 20:12

Paul Reiners


People also ask

How do I change the background color of a combobox in Java?

A JComboBox can generate an ActionListener, ChangeListener and ItemListener interfaces when the user actions on a combo box. We can also set the foreground and background color to JComboBox items by using setForeground() and setBackground() methods of a JComboBox class.

How do I make JComboBox not editable?

u can make ur jcombobox uneditable by calling its setEnabled(false). A JComboBox is unEditable by default. You can make it editable with the setEditable(boolean) command. If you are talking about enabled or disabled you can set that by the setEnabled(boolean) method.

Which of these is used to determine whether the JComboBox is editable?

SetEditable() - Determines whether the JComboBox field is editable.

Which event gets generated when you select an item from a JComboBox?

JComboBox() : creates a new empty JComboBox . JComboBox(ComboBoxModel M) : creates a new JComboBox with items from specified ComboBoxModel. JComboBox(E [ ] i) : creates a new JComboBox with items from specified array.


3 Answers

This should work

jComboBox1.setRenderer(new DefaultListCellRenderer() {
    @Override
    public void paint(Graphics g) {
        setBackground(Color.WHITE);
        setForeground(Color.BLACK);
        super.paint(g);
    }
});
like image 76
Costis Aivalis Avatar answered Sep 28 '22 15:09

Costis Aivalis


The background assigned by the renderer is overriden by the selection background color of the JList that is used in the popup for the combo box. Check out the "paintCurrentValue" method of the BasicComboBoxUI class. So the workaround would be:

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

This will affect the rendering of the popup as well. If you don't want it to affect the popup then you will need to create a custom renderer to specifically set the background of selected items.

like image 24
camickr Avatar answered Sep 28 '22 13:09

camickr


Have you tried writing your own, custom, ListCellRenderer?

When that method is asked to provide a cell-rendering component you get the following arguments:

 public Component getListCellRendererComponent(JList list,
                                               Object value,
                                               int index,
                                               boolean isSelected,
                                               boolean cellHasFocus) {
like image 40
aioobe Avatar answered Sep 28 '22 14:09

aioobe