I’m using a custom BasicComboBoxRenderer for a JComboBox and I’ve changed the appearance of the items of the drop-down list. However these changes also apply to the single top item that shows in the combobox (don’t know how to call it).
I want the top item to be independent of the other items in the list, if possible. I would also like to get rid of the top item’s blue color when it is focused (setFocusable(false) is not what I want).
I’ve tried to use the “renderer index” (-1) to affect the top item but it doesn’t seem to help.
Any ideas?
P.S Unfortunately I couldn’t add images to be more clear (no reputation).
EDIT: When I say that I want the top item to be independent from all the other items of the drop-down list I mean to always look different from the rest of them. For example in my custom BasicComboBoxRenderer I’ve set the selected item to have a different background, but this background also applies to the top item (since the selected item becomes the top item of the combobox).
EDIT 2: top item = I meant the combobox display area, so I want to affect the item that is shown at the display area and not the first item in the drop-down list. I managed to do this by using setBackground on the combobox itself AND setFocusable(false) (which is not very helpful because I want to keep the focus mechanism). But the problem is (except the focus issue) that if for example I set a border on each item in the list through a custom BasicComboBoxRenderer or ListCellRenderer class, this same border appears on the item that is shown in the display area. So there are 2 questions here:
–Is there any way to differentiate the layout of the items in the drop-down list and the single item in the display area?
–Is there any way to disable the focus color of the combobox without disabling the focus mechanism, just like when we use setFocusPainted(false) on buttons? (I’ve also tried to add a custom FocusListener on the combobox but any change made of the background through focusGained() affects only the button and not the item shown in the display area).
Sorry for the confusion and the multiple edits…
have look at Combo Box Prompt by @camickr,
defined prompt can’t returns any value from
JComboBox.getSelectedXxxEDIT
BasicComboBoxRenderer or ListCellRenderer can do that this way
EDIT2
JComboBox has two states
editable
non_editable
basically all values could be accesible from UIManager, shortcuts