Skip to main content

How to customize the JComboBox button icon ?

5 replies [Last post]
joejoe
Offline
Joined: 2005-07-08

Is there a way to customize the JComboBox button icon? Note, I'm not talking about a custom list renderer, I'm referring to the button on the right side.

Instead of the L&F choices, I want to customize the icon to be something different than what is available. I want the button to be much thinner and with a different icon.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
leouser
Offline
Joined: 2005-12-12

Im thinking you will have to create a custom Editor to deal with this:
setEditor(ComboBoxEditor anEditor)
Sets the editor used to paint and edit the selected item in the JComboBox field.

I can't remember off hand if you can control the button or not, Im somewhat suspecting not. Why? Well the ComboBoxEditor only is asking only for the Editor component via getEditorComponent(), I don't see any mention of a button anywhere. Too bad you aren't using Synth or you could probably deal with this easiliy

leouser

leouser
Offline
Joined: 2005-12-12

I just did this experiment in Jython:
1. Subclass MetalComboBoxUI.
2. override the createArrowButton
3. create a JComboBox

JComboBox jcb = new JComboBox()
MetalComboBoxUI2 ui2 = new MetalComboBoxUI2()
jcb.setUI( ui2 )

when displayed, instead of an arrow button my button had the text "Mooo", which is strange because I set it to say "Meow"???( joke--> It really was supposed to say "Mooo" ).

so in other words an angle of attack is to subclass MetalComboBoxUI and override createArrowButton to return the button with the Icon you want.

I hope there isn't something wrong with this approach,
leouser

joejoe
Offline
Joined: 2005-07-08

leouser:

Thanks for the idea. You are right, I can get what I want by changing the UI delegate. It seems like a rather large hammer just to change a button icon though. I don't want the metal or basic L&F. I just want to change the icon.

Here is another possible way:

Use a label and a button to get the rendering effect. Then display the combobox when the user clicks.

leouser
Offline
Joined: 2005-12-12

yes, the mega-widget approach should work as well. It very easy to build up the component you want out of the Swing widgets.

leouser

joejoe
Offline
Joined: 2005-07-08

I think I'm going to use your idea of subclassing BasicComboBoxUI and overriding createArrowButton(). Yes, it is a large hammer, but it seems a waste of my effort to rebuild something that already exists (JComboBox) from scratch. Anyway, thanks again for the idea.