<Swing Dev> 6303622: Generics: JComboBox: get/setSelectedItem(Object)

Florian Brunner fbrunnerlist at gmx.ch
Sun Dec 13 23:44:31 UTC 2009


Hi Pavel,

I start here a new thread for the "get/setSelectedItem(Object) methods of JComboBox and 
ComboBoxModel" discussion.

After further analysis of the code and your sample application I think we can and should generify 
the get/setSelectedItem(Object) methods of JComboBox and ComboBoxModel.

Yes, the Javadoc says that JComboBox/ ComboBoxModel supports selected values not managed by the 
underlying list model. But this does not prohibit to optionally limit the type by using generics und 
thus to increase type safety. 

If you need to allow other types from editor than the ones in the list model, you still can use:
JComboBox<Object> (or JComboBox, but this is not recommended)

So there should be no backward compatibility problem.

When using a JComboBox, usually you are interested in the selected value and since you want to do 
something with it you expect it to have some specific type. So if we generify the 
get/setSelectedItem(Object), you can profit from that in most cases.

Even in cases where you have an initial text in an editable combo box you can profit from that, if 
you use a "null" value as the selected value, which according to the API is used for "no selection", 
and a custom editor for rendering that null value. (see attachement; I used your sample application 
as a base; delete the text to set the selected value to null again).

-Florian
 



-------------- next part --------------
A non-text attachment was scrubbed...
Name: ComboTest.java
Type: text/x-java
Size: 3391 bytes
Desc: not available
URL: <http://mail.openjdk.java.net/pipermail/swing-dev/attachments/20091214/df092360/ComboTest.java>


More information about the swing-dev mailing list