Package com.jidesoft.swing
Class TableSearchable
- java.lang.Object
-
- com.jidesoft.swing.Searchable
-
- com.jidesoft.swing.TableSearchable
-
- All Implemented Interfaces:
java.beans.PropertyChangeListener,java.util.EventListener,javax.swing.event.TableModelListener
public class TableSearchable extends Searchable implements javax.swing.event.TableModelListener, java.beans.PropertyChangeListener
TableSearchableis an concrete implementation ofSearchablethat enables the search function in JTable.It's very simple to use it. Assuming you have a JTable, all you need to do is to call
As JTable is a two dimension data, the search is a little different from JList and JTree which both have one dimension data. So there is a little work you need to do in order to convert from two dimension data to one dimension data. We use the selection mode to determine how to convert. There is a special property called mainIndex. You can set it using setMainIndex(). If the JTable is in row selection mode, mainIndex will be the column that you want search at. Please note you can change mainIndex at any time. On the other hand, if the JTable is in column selection mode, mainIndex will be the row that you want search at. There is one more case when cell selection is enabled. In this case, mainIndex will be ignore; all cells will be searched. In three cases above, the keys for find next and find previous are different too. In row selection mode, up/down arrow are the keys. In column selection mode, left/right arrow are keys. In cell selection mode, both up and left arrow are keys to find previous occurrence, both down and right arrow are keys to find next occurrence. In addition, you might need to override convertElementToString() to provide you own algorithm to do the conversion.Now the JTable will have the search function.JTable table = ....; TableSearchable searchable = new TableSearchable(table);
Additional customization can be done on the base Searchable class such as background and foreground color, keystrokes, case sensitivity,JTable table = ....; TableSearchable searchable = new TableSearchable(table) { 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 TableSearchable(javax.swing.JTable table)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddTableSelection(javax.swing.JTable table, int rowIndex, int columnIndex, boolean incremental)Selects the cell at the specified row and column index.protected java.lang.StringconvertElementToString(java.lang.Object item)Converts the element that returns from getElementAt() to string.protected java.lang.ObjectgetElementAt(int index)Gets the element at the specified index.protected intgetElementCount()Gets the total element count in the component.intgetMainIndex()Gets the index of the column to be searched.int[]getSearchColumnIndices()Gets the indexes of the column to be searched.protected intgetSelectedIndex()Gets the selected index.protected java.lang.ObjectgetValueAt(javax.swing.JTable table, int rowIndex, int columnIndex)Get string value of the table.voidinstallListeners()Installs necessary listeners to the component.protected booleanisActivateKey(java.awt.event.KeyEvent e)Checks if the key in KeyEvent should activate the search popup.protected booleanisColumnSelectionAllowed(javax.swing.JTable table)Is the column selection allowed?protected booleanisFindNextKey(java.awt.event.KeyEvent e)Checks if the key is used as a key to find the next occurrence.protected booleanisFindPreviousKey(java.awt.event.KeyEvent e)Checks if the key is used as a key to find the previous occurrence.protected booleanisRowSelectionAllowed(javax.swing.JTable table)Is the row selection allowed?protected booleanisSearchSelectedRows()Are we trying to search on multi-columns (but NOT all columns)?protected booleanisSelectedCellEditable()Checks if the selected cell is editable.voidpropertyChange(java.beans.PropertyChangeEvent evt)voidsetMainIndex(int mainIndex)Sets the main index.voidsetSearchColumnIndices(int[] columnIndices)Sets the main indexes.protected voidsetSelectedIndex(int index, boolean incremental)Sets the selected index.voidtableChanged(javax.swing.event.TableModelEvent e)voiduninstallListeners()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, isCaseSensitive, isCountMatch, isDeactivateKey, isFindFirstKey, isFindLastKey, 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
-
installListeners
public void installListeners()
Description copied from class:SearchableInstalls necessary listeners to the component. This method will be called automatically when Searchable is created.- Overrides:
installListenersin classSearchable
-
uninstallListeners
public void uninstallListeners()
Description copied from class:SearchableUninstall 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:
uninstallListenersin classSearchable
-
setSelectedIndex
protected void setSelectedIndex(int index, boolean incremental)Description copied from class:SearchableSets 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:
setSelectedIndexin 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.
-
addTableSelection
protected void addTableSelection(javax.swing.JTable table, int rowIndex, int columnIndex, boolean incremental)Selects the cell at the specified row and column index. If incremental is true, the previous selection will not be cleared. This method will useJTable.changeSelection(int,int,boolean,boolean)method to select the cell if the row and column index is in the range and the cell was not selected. The last two parameters of changeSelection is true and false respectively.- Parameters:
table- the tablerowIndex- the row index of the cell.columnIndex- the column index of the cellincremental- false to clear all previous selection. True to keep the previous selection.
-
isColumnSelectionAllowed
protected boolean isColumnSelectionAllowed(javax.swing.JTable table)
Is the column selection allowed?- Parameters:
table- the table.- Returns:
- true if the table is the column selection.
-
isRowSelectionAllowed
protected boolean isRowSelectionAllowed(javax.swing.JTable table)
Is the row selection allowed?- Parameters:
table- the table.- Returns:
- true if the table is the row selection.
-
isSearchSelectedRows
protected boolean isSearchSelectedRows()
Are we trying to search on multi-columns (but NOT all columns)?- Returns:
- true if the search is set to look at multi-columns (but NOT all columns).
-
getSelectedIndex
protected int getSelectedIndex()
Gets the selected index.- Specified by:
getSelectedIndexin classSearchable- Returns:
- the selected index.
-
getElementAt
protected java.lang.Object getElementAt(int index)
Description copied from class:SearchableGets 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:
getElementAtin classSearchable- Parameters:
index- the index- Returns:
- the element at the specified index.
-
getValueAt
protected java.lang.Object getValueAt(javax.swing.JTable table, int rowIndex, int columnIndex)Get string value of the table.- Parameters:
table- the JTablerowIndex- the row indexcolumnIndex- the column index- Returns:
- the string value of the cell in the table.
-
getElementCount
protected int getElementCount()
Description copied from class:SearchableGets 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:
getElementCountin classSearchable- Returns:
- the total element count.
-
convertElementToString
protected java.lang.String convertElementToString(java.lang.Object item)
Description copied from class:SearchableConverts the element that returns from getElementAt() to string.- Specified by:
convertElementToStringin classSearchable- Parameters:
item- the element to be converted- Returns:
- the string representing the element in the component.
-
getSearchColumnIndices
public int[] getSearchColumnIndices()
Gets the indexes of the column to be searched.- Returns:
- the indexes of the column to be searched.
-
getMainIndex
public int getMainIndex()
Gets the index of the column to be searched.- Returns:
- the index of the column to be searched.
-
setSearchColumnIndices
public void setSearchColumnIndices(int[] columnIndices)
Sets the main indexes. Main indexes are the columns index which you want to be searched.- Parameters:
columnIndices- the index of the columns to be searched. If empty, all columns will be searched.
-
setMainIndex
public void setMainIndex(int mainIndex)
Sets the main index. Main index is the column index which you want to be searched.- Parameters:
mainIndex- the index of the column to be searched. If -1, all columns will be searched.
-
isFindNextKey
protected boolean isFindNextKey(java.awt.event.KeyEvent e)
Description copied from class:SearchableChecks if the key is used as a key to find the next occurrence.- Overrides:
isFindNextKeyin classSearchable- Parameters:
e- the key event- Returns:
- true if the key in KeyEvent is a key to find the next occurrence. By default, down arrow key is used.
-
isFindPreviousKey
protected boolean isFindPreviousKey(java.awt.event.KeyEvent e)
Description copied from class:SearchableChecks if the key is used as a key to find the previous occurrence.- Overrides:
isFindPreviousKeyin classSearchable- Parameters:
e- the key event- Returns:
- true if the key in KeyEvent is a key to find the previous occurrence. By default, up arrow key is used.
-
tableChanged
public void tableChanged(javax.swing.event.TableModelEvent e)
- Specified by:
tableChangedin interfacejavax.swing.event.TableModelListener
-
propertyChange
public void propertyChange(java.beans.PropertyChangeEvent evt)
- Specified by:
propertyChangein interfacejava.beans.PropertyChangeListener
-
isActivateKey
protected boolean isActivateKey(java.awt.event.KeyEvent e)
Description copied from class:SearchableChecks if the key in KeyEvent should activate the search popup.- Overrides:
isActivateKeyin classSearchable- Parameters:
e- the key event- Returns:
- true if the keyChar is visible except space and tab.
-
isSelectedCellEditable
protected boolean isSelectedCellEditable()
Checks if the selected cell is editable. If yes, we will not activate Searchable when key is typed.- Returns:
- true if the selected cell is editable.
-
-