Package com.jidesoft.swing
Class ComboBoxSearchable
- java.lang.Object
-
- com.jidesoft.swing.Searchable
-
- com.jidesoft.swing.ComboBoxSearchable
-
- All Implemented Interfaces:
java.beans.PropertyChangeListener
,java.util.EventListener
,javax.swing.event.ListDataListener
,javax.swing.event.PopupMenuListener
public class ComboBoxSearchable extends Searchable implements javax.swing.event.ListDataListener, java.beans.PropertyChangeListener, javax.swing.event.PopupMenuListener
ComboBoxSearchable
is an concrete implementation ofSearchable
that enables the search function in non-editable JComboBox.It's very simple to use it. Assuming you have a JComboBox, all you need to do is to call
There is very little customization you need to do to ComboBoxSearchable. The only thing you might need is when the element in the JComboBox needs a special conversion to convert to string. If so, you can override convertElementToString() to provide you own algorithm to do the conversion.
Now the JComboBox will have the search function.JComboBox comboBox = ....; ComboBoxSearchable searchable = new ComboBoxSearchable(comboBox);
Additional customization can be done on the base Searchable class such as background and foreground color, keystrokes, case sensitivity,JComboBox comboBox = ....; ComboBoxSearchable searchable = new ComboBoxSearchable(comboBox) { protected String convertElementToString(Object object) { ... } };
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.jidesoft.swing.Searchable
Searchable.DefaultSearchPopup, Searchable.SearchField, Searchable.SearchPopup
-
-
Field Summary
-
Fields inherited from class com.jidesoft.swing.Searchable
_component, _componentListener, _focusListener, _keyListener, _matchCount, CLIENT_PROPERTY_SEARCHABLE, listenerList, PROPERTY_SEARCH_TEXT
-
-
Constructor Summary
Constructors Constructor Description ComboBoxSearchable(javax.swing.JComboBox comboBox)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
contentsChanged(javax.swing.event.ListDataEvent e)
protected java.lang.String
convertElementToString(java.lang.Object object)
Converts the element in JCombobox 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
isRefreshPopupDuringSearching()
Checks if the popup should be refreshed during searching.boolean
isShowPopupDuringSearching()
Checks if the popup is showing during searching.void
popupMenuCanceled(javax.swing.event.PopupMenuEvent e)
void
popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent e)
void
popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent e)
void
propertyChange(java.beans.PropertyChangeEvent evt)
void
setRefreshPopupDuringSearching(boolean refreshPopupDuringSearching)
Sets the property which determines if the popup should be refreshed during searching.protected void
setSelectedIndex(int index, boolean incremental)
Sets the selected index.void
setShowPopupDuringSearching(boolean showPopupDuringSearching)
Sets the property which determines if the popup should be shown during searching.void
uninstallListeners()
Uninstall the listeners that installed before.-
Methods inherited from class com.jidesoft.swing.Searchable
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, textChanged
-
-
-
-
Method Detail
-
uninstallListeners
public void uninstallListeners()
Description copied from class:Searchable
Uninstall the listeners that installed before. This method is never called because we don't have the control of the life cycle of the component. However you can call this method if you don't want the component to be searchable any more.- Overrides:
uninstallListeners
in classSearchable
-
isShowPopupDuringSearching
public boolean isShowPopupDuringSearching()
Checks if the popup is showing during searching.- Returns:
- true if popup is visible during searching.
-
setShowPopupDuringSearching
public void setShowPopupDuringSearching(boolean showPopupDuringSearching)
Sets the property which determines if the popup should be shown during searching.- Parameters:
showPopupDuringSearching
- the flag indicating if we should show popup during searching
-
isRefreshPopupDuringSearching
public boolean isRefreshPopupDuringSearching()
Checks if the popup should be refreshed during searching. By default, the value is false. ComboBoxShrinkSearchSupport will set this flag to true.- Returns:
- true if popup is refreshed during searching.
-
setRefreshPopupDuringSearching
public void setRefreshPopupDuringSearching(boolean refreshPopupDuringSearching)
Sets the property which determines if the popup should be refreshed during searching.- Parameters:
refreshPopupDuringSearching
- the flag indicating if we should refresh popup during searching
-
setSelectedIndex
protected void setSelectedIndex(int index, boolean incremental)
Description copied from class:Searchable
Sets the selected index. The concrete implementation should call methods on the component to select the element at the specified index. The incremental flag is used to do multiple select. 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.- Specified by:
setSelectedIndex
in classSearchable
- Parameters:
index
- 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.
-
getSelectedIndex
protected int getSelectedIndex()
Description copied from class:Searchable
Gets the selected index in the component. The concrete implementation should call methods on the component to retrieve the current selected index. If the component supports multiple selection, it's OK just return the index of the first selection.Here 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).
- Specified by:
getSelectedIndex
in classSearchable
- Returns:
- the selected index.
-
getElementAt
protected java.lang.Object getElementAt(int index)
Description copied from class:Searchable
Gets the element at the specified index. The element could be any data structure that internally used in the component. The convertElementToString method will give you a chance to convert the element to string which is used to compare with the string that user types in.- Specified by:
getElementAt
in classSearchable
- Parameters:
index
- the index- Returns:
- the element at the specified index.
-
getElementCount
protected int getElementCount()
Description copied from class:Searchable
Gets the total element count in the component. Different concrete implementation could have different interpretation of the count. This is totally OK as long as it's consistent in all the methods. For example, the index parameter in other methods should be always a valid value within the total count.- Specified by:
getElementCount
in classSearchable
- Returns:
- the total element count.
-
convertElementToString
protected java.lang.String convertElementToString(java.lang.Object object)
Converts the element in JCombobox to string. The returned value will be thetoString()
of whatever element that returned fromlist.getModel().getElementAt(i)
.- Specified by:
convertElementToString
in classSearchable
- Parameters:
object
- the object to be converted- Returns:
- the string representing the element in the JComboBox.
-
contentsChanged
public void contentsChanged(javax.swing.event.ListDataEvent e)
- Specified by:
contentsChanged
in interfacejavax.swing.event.ListDataListener
-
intervalAdded
public void intervalAdded(javax.swing.event.ListDataEvent e)
- Specified by:
intervalAdded
in interfacejavax.swing.event.ListDataListener
-
intervalRemoved
public void intervalRemoved(javax.swing.event.ListDataEvent e)
- Specified by:
intervalRemoved
in interfacejavax.swing.event.ListDataListener
-
propertyChange
public void propertyChange(java.beans.PropertyChangeEvent evt)
- Specified by:
propertyChange
in interfacejava.beans.PropertyChangeListener
-
popupMenuWillBecomeVisible
public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent e)
- Specified by:
popupMenuWillBecomeVisible
in interfacejavax.swing.event.PopupMenuListener
-
popupMenuWillBecomeInvisible
public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent e)
- Specified by:
popupMenuWillBecomeInvisible
in interfacejavax.swing.event.PopupMenuListener
-
popupMenuCanceled
public void popupMenuCanceled(javax.swing.event.PopupMenuEvent e)
- Specified by:
popupMenuCanceled
in interfacejavax.swing.event.PopupMenuListener
-
-