devxlogo

Customizing an Editable JComboBox

Customizing an Editable JComboBox

A previous tip explained how to change the appearance of the ‘list selection’ cells of a JComboBox by setting the attributes of the JComboBox object’s ListCellRenderer.
However, the ListCellRenderer controls the item area for a non-editable JcomboBox. A ComboBoxEditor controls the item area for an editable JcomboBox (which, in turn, contains a component that handles the visual aspect of the JComboBox.) Here is how you might change the background color of the editable area of a JComboBox:

 ComboBoxEditor cbe = cb.getEditor();Component c = cbe.getEditorComponent();c.setBackground(Color.red);

size=3>
When the JComboBox is not editable (which is the default) then the standard method ‘cb.setBackground(…)’ will work, but it has the side effect of also changing the background colors of the arrow button and the individual unselected cells in the selection list area. Setting the color on the default ListCellRenderer has no effect because the renderer’s colors are adjusted during repainting (remember

devx-admin

Share the Post: