Class EnumComboBoxModel<E extends Enum<E>>
- All Implemented Interfaces:
ActionListener,Serializable,EventListener,ComboBoxModel<E>,ListModel<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 the
EnumComboBoxModel and then set it as the model on the combo
box. The combo box will then appear on screen with each value in the
enum 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:
private enum MyEnum {GoodStuff, BadStuff;
public String toString() {
switch(this) {
case GoodStuff: return "Some Good Stuff";
case BadStuff: return "Some Bad Stuff";
}
return "ERROR";
}
};
Note: if more than one enum constant returns the same String via
toString(), this model will throw an exception on creation.- Author:
- joshy, Karl Schaefer
- See Also:
-
Field Summary
Fields inherited from class org.jdesktop.swingx.combobox.ListComboBoxModel
data, selected, UPDATEFields inherited from class javax.swing.AbstractListModel
listenerList -
Constructor Summary
ConstructorsConstructorDescriptionEnumComboBoxModel(Class<E> en) Creates anEnumComboBoxModelfor the enum represent by theClassen. -
Method Summary
Methods inherited from class org.jdesktop.swingx.combobox.ListComboBoxModel
actionPerformed, getElementAt, getSelectedItem, getSizeMethods inherited from class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListenerMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.swing.ListModel
addListDataListener, removeListDataListener
-
Constructor Details
-
EnumComboBoxModel
Creates anEnumComboBoxModelfor the enum represent by theClassen.- Parameters:
en- the enum class type- Throws:
IllegalArgumentException- if theEnum.toStringreturns the same value for more than one constant
-
-
Method Details
-
setSelectedItem
Set the selected item. The implementation of this method should notify all registeredListDataListeners that the contents have changed.- Specified by:
setSelectedItemin interfaceComboBoxModel<E extends Enum<E>>- Overrides:
setSelectedItemin classListComboBoxModel<E extends Enum<E>>- Parameters:
anItem- the list object to select ornullto clear the selection
-