Package org.jfree.ui
Class KeyedComboBoxModel
- java.lang.Object
-
- org.jfree.ui.KeyedComboBoxModel
-
- All Implemented Interfaces:
javax.swing.ComboBoxModel,javax.swing.ListModel
public class KeyedComboBoxModel extends java.lang.Object implements javax.swing.ComboBoxModel
The KeyedComboBox model allows to define an internal key (the data element) for every entry in the model.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.
- Author:
- Thomas Morgner
-
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(java.lang.Object key, java.lang.Object cbitem)Adds a new entry to the model.voidaddListDataListener(javax.swing.event.ListDataListener l)Adds a listener to the list that's notified each time a change to the data model occurs.voidclear()Removes all entries from the model.intfindElementIndex(java.lang.Object key)Tries to find the index of element with the given key.protected voidfireListDataEvent(javax.swing.event.ListDataEvent evt)Notifies all registered list data listener of the given event.java.lang.ObjectgetElementAt(int index)Returns the value at the specified index.java.lang.ObjectgetKeyAt(int index)Returns the key from the given index.java.lang.ObjectgetSelectedItem()Returns the selected item.java.lang.ObjectgetSelectedKey()Returns the selected data element or null if none is set.intgetSize()Returns the length of the list.voidremoveDataElement(java.lang.Object key)Removes an entry from the model.voidremoveListDataListener(javax.swing.event.ListDataListener l)Removes a listener from the list that's notified each time a change to the data model occurs.voidsetAllowOtherValue(boolean allowOtherValue)voidsetData(java.lang.Object[] keys, java.lang.Object[] values)Replaces the data in this combobox model.voidsetSelectedItem(java.lang.Object anItem)Set the selected item.voidsetSelectedKey(java.lang.Object anItem)Defines the selected key.
-
-
-
Constructor Detail
-
KeyedComboBoxModel
public KeyedComboBoxModel()
Creates a new keyed combobox model.
-
KeyedComboBoxModel
public KeyedComboBoxModel(java.lang.Object[] keys, java.lang.Object[] values)
Creates a new keyed combobox model for the given keys and values. Keys and values must have the same number of items.- Parameters:
keys- the keysvalues- the values
-
-
Method Detail
-
setData
public void setData(java.lang.Object[] keys, java.lang.Object[] values)
Replaces the data in this combobox model. The number of keys must be equals to the number of values.- Parameters:
keys- the keysvalues- the values
-
fireListDataEvent
protected void fireListDataEvent(javax.swing.event.ListDataEvent evt)
Notifies all registered list data listener of the given event.- Parameters:
evt- the event.
-
getSelectedItem
public java.lang.Object getSelectedItem()
Returns the selected item.- Specified by:
getSelectedItemin interfacejavax.swing.ComboBoxModel- Returns:
- The selected item or
nullif there is no selection
-
setSelectedKey
public void setSelectedKey(java.lang.Object anItem)
Defines the selected key. If the object is not in the list of values, no item gets selected.- Parameters:
anItem- the new selected item.
-
setSelectedItem
public void setSelectedItem(java.lang.Object anItem)
Set the selected item. The implementation of this method should notify all registeredListDataListeners that the contents have changed.- Specified by:
setSelectedItemin interfacejavax.swing.ComboBoxModel- Parameters:
anItem- the list object to select ornullto clear the selection
-
setAllowOtherValue
public void setAllowOtherValue(boolean allowOtherValue)
- Parameters:
allowOtherValue- new flag value.
-
addListDataListener
public 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.- Specified by:
addListDataListenerin interfacejavax.swing.ListModel- Parameters:
l- theListDataListenerto be added
-
getElementAt
public java.lang.Object getElementAt(int index)
Returns the value at the specified index.- Specified by:
getElementAtin interfacejavax.swing.ListModel- Parameters:
index- the requested index- Returns:
- the value at
index
-
getKeyAt
public java.lang.Object getKeyAt(int index)
Returns the key from the given index.- Parameters:
index- the index of the key.- Returns:
- the the key at the specified index.
-
getSelectedKey
public java.lang.Object getSelectedKey()
Returns the selected data element or null if none is set.- Returns:
- the selected data element.
-
getSize
public int getSize()
Returns the length of the list.- Specified by:
getSizein interfacejavax.swing.ListModel- Returns:
- the length of the list
-
removeListDataListener
public 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.- Specified by:
removeListDataListenerin interfacejavax.swing.ListModel- Parameters:
l- theListDataListenerto be removed
-
findElementIndex
public int findElementIndex(java.lang.Object key)
Tries to find the index of element with the given key. The key must not be null.- Parameters:
key- the key for the element to be searched.- Returns:
- the index of the key, or -1 if not found.
-
removeDataElement
public void removeDataElement(java.lang.Object key)
Removes an entry from the model.- Parameters:
key- the key
-
add
public void add(java.lang.Object key, java.lang.Object cbitem)
Adds a new entry to the model.- Parameters:
key- the keycbitem- the display value.
-
clear
public void clear()
Removes all entries from the model.
-
-