public class ListSearchable extends Searchable implements javax.swing.event.ListDataListener, java.beans.PropertyChangeListener
ListSearchable is an concrete implementation of Searchable that enables the search function in
JList. It's very simple to use it. Assuming you have a JList, all you need to do is to call
Now the JList will have the search function.
JList list = ....;
ListSearchable searchable = new ListSearchable(list);
JList list = ....;
ListSearchable searchable = new ListSearchable(list) {
protected String convertElementToString(Object object) {
...
}
};
Additional customization can be done on the base Searchable class such as background and foreground color,
keystrokes, case sensitivity.
JList actually has a simple searchable feature but has flaws. It will affect our searchable feature. To workaround
it, you can override getNextMatch method and always return -1 when you create your JList.
JList list = new JList(...) {
public int getNextMatch(String prefix, int startIndex, Position.Bias bias) {
return -1;
}
};
Searchable.DefaultSearchPopup, Searchable.SearchField, Searchable.SearchPopup_component, _componentListener, _focusListener, _keyListener, _matchCount, CLIENT_PROPERTY_SEARCHABLE, listenerList, PROPERTY_SEARCH_TEXT| Constructor and Description |
|---|
ListSearchable(javax.swing.JList list) |
| Modifier and Type | Method and Description |
|---|---|
void |
contentsChanged(javax.swing.event.ListDataEvent e) |
protected java.lang.String |
convertElementToString(java.lang.Object object)
Converts the element in Jlist to string.
|
protected java.lang.Object |
getElementAt(int index)
Gets the element at the specified index.
|
protected int |
getElementCount()
Gets the total element count in the component.
|
protected int |
getSelectedIndex()
Gets the selected index in the component.
|
void |
intervalAdded(javax.swing.event.ListDataEvent e) |
void |
intervalRemoved(javax.swing.event.ListDataEvent e) |
boolean |
isUseRendererAsConverter()
Get the flag if the ListSearchable should use the renderer in the list as its converter.
|
void |
propertyChange(java.beans.PropertyChangeEvent evt) |
void |
setSelectedIndex(int index,
boolean incremental)
Sets the selected index.
|
void |
setUseRendererAsConverter(boolean useRendererAsConverter)
Set the flag if the ListSearchable should use the renderer in the list as its converter.
|
void |
uninstallListeners()
Uninstall the listeners that installed before.
|
addPropertyChangeListener, addSearchableListener, adjustSelectedIndex, cancelHighlightAll, compare, compare, convertToString, createComponentListener, createFocusListener, createKeyListener, createSearchPopup, findAll, findFirst, findFirstExactly, findFromCursor, findLast, findNext, findPrevious, firePropertyChangeEvent, fireSearchableEvent, getBackground, getComponent, getCurrentIndex, getCursor, getElementAtAsString, getForeground, getMismatchForeground, getPopupLocation, getPopupLocationRelativeTo, getPopupTimeout, getResourceString, getSearchable, getSearchableListeners, getSearchableProvider, getSearchingDelay, getSearchingText, getSearchLabel, getWildcardSupport, hidePopup, highlightAll, installListeners, isActivateKey, isCaseSensitive, isCountMatch, isDeactivateKey, isFindFirstKey, isFindLastKey, isFindNextKey, isFindPreviousKey, isFromStart, isHeavyweightComponentEnabled, isHideSearchPopupOnEvent, isIncrementalSelectKey, isNavigationKey, isPopupVisible, isProcessModelChangeEvent, isRepeats, isReverseOrder, isSearchableListenerInstalled, isSelectAllKey, isWildcardEnabled, keyTypedOrPressed, removePropertyChangeListener, removeSearchableListener, reverseFindFromCursor, searchingTextEmpty, select, setBackground, setCaseSensitive, setCountMatch, setCursor, setCursor, setForeground, setFromStart, setHeavyweightComponentEnabled, setHideSearchPopupOnEvent, setMismatchForeground, setPopupLocation, setPopupLocationRelativeTo, setPopupTimeout, setProcessModelChangeEvent, setRepeats, setReverseOrder, setSearchableProvider, setSearchingDelay, setSearchLabel, setWildcardEnabled, setWildcardSupport, showPopup, textChangedpublic void uninstallListeners()
SearchableuninstallListeners in class Searchablepublic void setSelectedIndex(int index,
boolean incremental)
SearchablesetSelectedIndex in class Searchableindex - the index to be selectedincremental - a flag to enable multiple selection. If the flag is true, the element at the index should be
added to current selection. If false, you should clear previous selection and then select the
element.protected int getSelectedIndex()
SearchableHere are some examples. In the case of JList, the index is the row index. In the case of JTree, the index is the row index too. In the case of JTable, depending on the selection mode, the index could be row index (in row selection mode), could be column index (in column selection mode) or could the cell index (in cell selection mode).
getSelectedIndex in class Searchableprotected java.lang.Object getElementAt(int index)
SearchablegetElementAt in class Searchableindex - the indexprotected int getElementCount()
SearchablegetElementCount in class Searchableprotected java.lang.String convertElementToString(java.lang.Object object)
toString() of whatever
element that returned from list.getModel().getElementAt(i).convertElementToString in class Searchableobject - the object to be converted to stringpublic void contentsChanged(javax.swing.event.ListDataEvent e)
contentsChanged in interface javax.swing.event.ListDataListenerpublic void intervalAdded(javax.swing.event.ListDataEvent e)
intervalAdded in interface javax.swing.event.ListDataListenerpublic void intervalRemoved(javax.swing.event.ListDataEvent e)
intervalRemoved in interface javax.swing.event.ListDataListenerpublic void propertyChange(java.beans.PropertyChangeEvent evt)
propertyChange in interface java.beans.PropertyChangeListenerpublic boolean isUseRendererAsConverter()
public void setUseRendererAsConverter(boolean useRendererAsConverter)
useRendererAsConverter - the flagisUseRendererAsConverter()