|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavax.swing.AbstractListModel
org.jdesktop.swingx.combobox.EnumComboBoxModel<E>
public class EnumComboBoxModel<E extends Enum<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.
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));
By using generics and co-variant types you can make accessing elements from the model be completely typesafe. ex:
EnumComboBoxModel enumModel = new EnumComboBoxModel(MyEnum1.class);
MyEnum first = enumModel.getElement(0);
MyEnum selected = enumModel.getSelectedItem();
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";
}
};
Field Summary |
---|
Fields inherited from class javax.swing.AbstractListModel |
---|
listenerList |
Constructor Summary | |
---|---|
EnumComboBoxModel(Class<E> en)
|
Method Summary | |
---|---|
E |
getElementAt(int index)
Returns the value at the specified index. |
E |
getSelectedItem()
Returns the selected item |
int |
getSize()
Returns the length of the list. |
void |
setSelectedItem(Object anItem)
Set the selected item. |
Methods inherited from class javax.swing.AbstractListModel |
---|
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface javax.swing.ListModel |
---|
addListDataListener, removeListDataListener |
Constructor Detail |
---|
public EnumComboBoxModel(Class<E> en)
Method Detail |
---|
public int getSize()
javax.swing.ListModel
getSize
in interface ListModel
public E getElementAt(int index)
javax.swing.ListModel
getElementAt
in interface ListModel
index
- the requested index
index
public void setSelectedItem(Object anItem)
javax.swing.ComboBoxModel
ListDataListener
s that the contents
have changed.
setSelectedItem
in interface ComboBoxModel
anItem
- the list object to select or null
to clear the selectionpublic E getSelectedItem()
javax.swing.ComboBoxModel
getSelectedItem
in interface ComboBoxModel
null
if there is no selection
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |