JCombobox selected item background colour
When traversing from a JTextfield to JComboBox the FocusGained event is triggered so as to set the components background colour to green, this readily identifies the selected window component with ease for the user.
This works fine expect for the JComboBox. I cannot determine how to set the selected item background colour to green. However, I have managed to augment the background colour of the complete JComboBox to green but this isn't the requirement.
Comments, pointers would be much appreciated.
[523 byte] By [
sweenya] at [2007-10-3 3:37:18]

> I cannot determine how to set the selected item background colour to green
Using the setBackground(...) method will changed the background of the comboBox and the drop down list. If the comboBox does not change this would be because you are using an editable comboBox. In this case you need to set the background color of the editor used by the comboBox.
comboBox.getEditor().getEditorComponent.setBackground(...);
In case your interested, this posting shows how to fade the color in and out when it gains/loses focus:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=634810
The code which has been suggested as the possible solution for changing the background colour of a combobox selected item
comboBox.getEditor().getEditorComponent.setBackground(Color.green);
has not proven successful. I have also looked at the suggested linked article regarding the fader colour class but unfortunately this code is over and above my coding ability and unfortunately do not understand it.
I have recently found this link
http://forum.java.sun.com/thread.jspa?threadID=753194
titled: Changing background of selected item in JComboBox when Uneditable
This example offers a potential solution as the combobox currently used is a none editable one, therefore the render handles the selected combobox item rather than an editor.
From the code in the above mentioned link, the selected item background and combobox background colours can be augmented independently of one another.
What I would now like to know is, how to change the currently displayed combobox item background colour when focus is gained yet no selection has been made?
> how to change the currently displayed combobox item background colour when focus is gained yet no selection has been made?
I don't understand the question. Are you saying you want two different colors displayed when focus is gained?
a) one color when no item is selected
b) one color when an item is selected
If indeed you are saying you want two differenct background colors then, as we know from the above link the comboBox background is set from the setBackground(...) method. The background color of the dropdown list can be changed by providing a custom renderer.
So in the renderer from above you can check for a non selected value by using
if (value == null)
setBackground(...);
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
The solution I have found is culminated from both the sun website forums and google groups.
When the JComboBox attains focus the displayed background colour is changed to green and also when the user searches through the dropdown list of items, each highlighted item is identified in green as per code hereunder:
Once focus is lost the JComboBox resumes it抯 original default colour:
private void jcbCoursesFocusGained (java.awt.event.FocusEvent evt)
{
defaultColour = jcbCourses.getBackground ();
jcbCourses.setRenderer (new DefaultListCellRenderer ()
{
public Component getListCellRendererComponent (JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
Component c = super.getListCellRendererComponent (list, value,
index,
isSelected,
cellHasFocus);
//sets selected item background colour
if(isSelected)
c.setBackground (Color.green);
//sets the displayed item background colour
list.setSelectionBackground (Color.green);
return c;
}
});
}
private void jcbCoursesFocusLost (java.awt.event.FocusEvent evt)
{
jcbCourses.setBackground (defaultColour);
}
