Class EnumComboBoxModel<E extends java.lang.Enum<E>>
- java.lang.Object
-
- javax.swing.AbstractListModel
-
- org.jdesktop.swingx.combobox.ListComboBoxModel<E>
-
- org.jdesktop.swingx.combobox.EnumComboBoxModel<E>
-
- All Implemented Interfaces:
java.awt.event.ActionListener
,java.io.Serializable
,java.util.EventListener
,javax.swing.ComboBoxModel
,javax.swing.ListModel
public class EnumComboBoxModel<E extends java.lang.Enum<E>> extends ListComboBoxModel<E>
A ComboBoxModel implementation that safely wraps an Enum. It allows the developer to directly use an enum as their model for a combobox without any extra work, though the display can can be further customized.
Simple Usage
The simplest usage is to wrap an
enum
inside theEnumComboBoxModel
and then set it as the model on the combo box. The combo box will then appear on screen with each value in theenum
as a value in the combobox.ex:
enum MyEnum { GoodStuff, BadStuff }; ... JComboBox combo = new JComboBox(); combo.setModel(new EnumComboBoxModel(MyEnum.class));
Type safe access
By using generics and co-variant types you can make accessing elements from the model be completely typesafe. ex:
EnumComboBoxModel<MyEnum> enumModel = new EnumComboBoxModel<MyEnum1>( MyEnum1.class); MyEnum first = enumModel.getElement(0); MyEnum selected = enumModel.getSelectedItem();
Advanced Usage
Since the exact
toString()
value of each enum constant may not be exactly what you want on screen (the values won't have spaces, for example) you can override to toString() method on the values when you declare your enum. Thus the display value is localized to the enum and not in your GUI code. ex:
Note: if more than one enum constant returns the sameprivate enum MyEnum {GoodStuff, BadStuff; public String toString() { switch(this) { case GoodStuff: return "Some Good Stuff"; case BadStuff: return "Some Bad Stuff"; } return "ERROR"; } };
String
viatoString()
, this model will throw an exception on creation.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Class<E>
enumClass
private static long
serialVersionUID
private java.util.Map<java.lang.String,E>
valueMap
-
Fields inherited from class org.jdesktop.swingx.combobox.ListComboBoxModel
data, selected, UPDATE
-
-
Constructor Summary
Constructors Constructor Description EnumComboBoxModel(java.lang.Class<E> en)
Creates anEnumComboBoxModel
for the enum represent by theClass
en
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
setSelectedItem(java.lang.Object anItem)
Set the selected item.-
Methods inherited from class org.jdesktop.swingx.combobox.ListComboBoxModel
actionPerformed, getElementAt, getSelectedItem, getSize
-
Methods inherited from class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
EnumComboBoxModel
public EnumComboBoxModel(java.lang.Class<E> en)
Creates anEnumComboBoxModel
for the enum represent by theClass
en
.- Parameters:
en
- the enum class type- Throws:
java.lang.IllegalArgumentException
- if theEnum.toString
returns the same value for more than one constant
-
-
Method Detail
-
setSelectedItem
public void setSelectedItem(java.lang.Object anItem)
Set the selected item. The implementation of this method should notify all registeredListDataListener
s that the contents have changed.- Specified by:
setSelectedItem
in interfacejavax.swing.ComboBoxModel<E extends java.lang.Enum<E>>
- Overrides:
setSelectedItem
in classListComboBoxModel<E extends java.lang.Enum<E>>
- Parameters:
anItem
- the list object to select ornull
to clear the selection
-
-