public class KeyedComboBoxModel
extends java.lang.Object
implements javax.swing.ComboBoxModel
This class is useful in all cases, where the public text differs from the internal view on the data. A separation between presentation data and processing data is a prerequisite for localizing combobox entries. This model does not allow selected elements, which are not in the list of valid elements.
Modifier and Type | Class and Description |
---|---|
private static class |
KeyedComboBoxModel.ComboBoxItemPair
The internal data carrier to map keys to values and vice versa.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
allowOtherValue |
private java.util.ArrayList |
data
The data (contains ComboBoxItemPairs).
|
private java.util.ArrayList |
listdatalistener
The listeners.
|
private int |
selectedItemIndex
The index of the selected item.
|
private java.lang.Object |
selectedItemValue |
private javax.swing.event.ListDataListener[] |
tempListeners
The cached listeners as array.
|
Constructor and Description |
---|
KeyedComboBoxModel()
Creates a new keyed combobox model.
|
KeyedComboBoxModel(java.lang.Object[] keys,
java.lang.Object[] values)
Creates a new keyed combobox model for the given keys and values.
|
Modifier and Type | Method and Description |
---|---|
void |
add(java.lang.Object key,
java.lang.Object cbitem)
Adds a new entry to the model.
|
void |
addListDataListener(javax.swing.event.ListDataListener l)
Adds a listener to the list that's notified each time a change to the data
model occurs.
|
void |
clear()
Removes all entries from the model.
|
private int |
findDataElementIndex(java.lang.Object anItem)
Searches an element by its data value.
|
int |
findElementIndex(java.lang.Object key)
Tries to find the index of element with the given key.
|
protected void |
fireListDataEvent(javax.swing.event.ListDataEvent evt)
Notifies all registered list data listener of the given event.
|
java.lang.Object |
getElementAt(int index)
Returns the value at the specified index.
|
java.lang.Object |
getKeyAt(int index)
Returns the key from the given index.
|
java.lang.Object |
getSelectedItem()
Returns the selected item.
|
java.lang.Object |
getSelectedKey()
Returns the selected data element or null if none is set.
|
int |
getSize()
Returns the length of the list.
|
private boolean |
isAllowOtherValue() |
void |
removeDataElement(java.lang.Object key)
Removes an entry from the model.
|
void |
removeListDataListener(javax.swing.event.ListDataListener l)
Removes a listener from the list that's notified each time a change to
the data model occurs.
|
void |
setAllowOtherValue(boolean allowOtherValue) |
void |
setData(java.lang.Object[] keys,
java.lang.Object[] values)
Replaces the data in this combobox model.
|
void |
setSelectedItem(java.lang.Object anItem)
Set the selected item.
|
void |
setSelectedKey(java.lang.Object anItem)
Defines the selected key.
|
private int selectedItemIndex
private java.lang.Object selectedItemValue
private java.util.ArrayList data
private java.util.ArrayList listdatalistener
private transient javax.swing.event.ListDataListener[] tempListeners
private boolean allowOtherValue
public KeyedComboBoxModel()
public KeyedComboBoxModel(java.lang.Object[] keys, java.lang.Object[] values)
keys
- the keysvalues
- the valuespublic void setData(java.lang.Object[] keys, java.lang.Object[] values)
keys
- the keysvalues
- the valuesprotected void fireListDataEvent(javax.swing.event.ListDataEvent evt)
evt
- the event.public java.lang.Object getSelectedItem()
getSelectedItem
in interface javax.swing.ComboBoxModel
null
if there is no selectionpublic void setSelectedKey(java.lang.Object anItem)
anItem
- the new selected item.public void setSelectedItem(java.lang.Object anItem)
ListDataListener
s that the contents have
changed.setSelectedItem
in interface javax.swing.ComboBoxModel
anItem
- the list object to select or null
to clear the
selectionprivate boolean isAllowOtherValue()
public void setAllowOtherValue(boolean allowOtherValue)
allowOtherValue
- new flag value.public void addListDataListener(javax.swing.event.ListDataListener l)
addListDataListener
in interface javax.swing.ListModel
l
- the ListDataListener
to be addedpublic java.lang.Object getElementAt(int index)
getElementAt
in interface javax.swing.ListModel
index
- the requested indexindex
public java.lang.Object getKeyAt(int index)
index
- the index of the key.public java.lang.Object getSelectedKey()
public int getSize()
getSize
in interface javax.swing.ListModel
public void removeListDataListener(javax.swing.event.ListDataListener l)
removeListDataListener
in interface javax.swing.ListModel
l
- the ListDataListener
to be removedprivate int findDataElementIndex(java.lang.Object anItem)
anItem
- the itempublic int findElementIndex(java.lang.Object key)
key
- the key for the element to be searched.public void removeDataElement(java.lang.Object key)
key
- the keypublic void add(java.lang.Object key, java.lang.Object cbitem)
key
- the keycbitem
- the display value.public void clear()