public class TableSearchable extends Searchable implements javax.swing.event.TableModelListener, java.beans.PropertyChangeListener
TableSearchable is an concrete implementation of Searchable that 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
Now the JTable will have the search function.
JTable table = ....;
TableSearchable searchable = new TableSearchable(table);
JTable table = ....;
TableSearchable searchable = new TableSearchable(table) {
protected String convertElementToString(Object object) {
...
}
};
Additional customization can be done on the base Searchable class such as background and foreground color,
keystrokes, case sensitivity,Searchable.DefaultSearchPopup, Searchable.SearchField, Searchable.SearchPopup_component, _componentListener, _focusListener, _keyListener, _matchCount, CLIENT_PROPERTY_SEARCHABLE, listenerList, PROPERTY_SEARCH_TEXT| Constructor and Description |
|---|
TableSearchable(javax.swing.JTable table) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addTableSelection(javax.swing.JTable table,
int rowIndex,
int columnIndex,
boolean incremental)
Selects the cell at the specified row and column index.
|
protected java.lang.String |
convertElementToString(java.lang.Object item)
Converts the element that returns from getElementAt() 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.
|
int |
getMainIndex()
Gets the index of the column to be searched.
|
int[] |
getSearchColumnIndices()
Gets the indexes of the column to be searched.
|
protected int |
getSelectedIndex()
Gets the selected index.
|
protected java.lang.Object |
getValueAt(javax.swing.JTable table,
int rowIndex,
int columnIndex)
Get string value of the table.
|
void |
installListeners()
Installs necessary listeners to the component.
|
protected boolean |
isActivateKey(java.awt.event.KeyEvent e)
Checks if the key in KeyEvent should activate the search popup.
|
protected boolean |
isColumnSelectionAllowed(javax.swing.JTable table)
Is the column selection allowed?
|
protected boolean |
isFindNextKey(java.awt.event.KeyEvent e)
Checks if the key is used as a key to find the next occurrence.
|
protected boolean |
isFindPreviousKey(java.awt.event.KeyEvent e)
Checks if the key is used as a key to find the previous occurrence.
|
protected boolean |
isRowSelectionAllowed(javax.swing.JTable table)
Is the row selection allowed?
|
protected boolean |
isSearchSelectedRows()
Are we trying to search on multi-columns (but NOT all columns)?
|
protected boolean |
isSelectedCellEditable()
Checks if the selected cell is editable.
|
void |
propertyChange(java.beans.PropertyChangeEvent evt) |
void |
setMainIndex(int mainIndex)
Sets the main index.
|
void |
setSearchColumnIndices(int[] columnIndices)
Sets the main indexes.
|
protected void |
setSelectedIndex(int index,
boolean incremental)
Sets the selected index.
|
void |
tableChanged(javax.swing.event.TableModelEvent e) |
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, 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, textChangedpublic void installListeners()
SearchableinstallListeners in class Searchablepublic void uninstallListeners()
SearchableuninstallListeners in class Searchableprotected 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 void addTableSelection(javax.swing.JTable table,
int rowIndex,
int columnIndex,
boolean incremental)
JTable.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.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.protected boolean isColumnSelectionAllowed(javax.swing.JTable table)
table - the table.protected boolean isRowSelectionAllowed(javax.swing.JTable table)
table - the table.protected boolean isSearchSelectedRows()
protected int getSelectedIndex()
getSelectedIndex in class Searchableprotected java.lang.Object getElementAt(int index)
SearchablegetElementAt in class Searchableindex - the indexprotected java.lang.Object getValueAt(javax.swing.JTable table,
int rowIndex,
int columnIndex)
table - the JTablerowIndex - the row indexcolumnIndex - the column indexprotected int getElementCount()
SearchablegetElementCount in class Searchableprotected java.lang.String convertElementToString(java.lang.Object item)
SearchableconvertElementToString in class Searchableitem - the element to be convertedpublic int[] getSearchColumnIndices()
public int getMainIndex()
public void setSearchColumnIndices(int[] columnIndices)
columnIndices - the index of the columns to be searched. If empty, all columns will be searched.public void setMainIndex(int mainIndex)
mainIndex - the index of the column to be searched. If -1, all columns will be searched.protected boolean isFindNextKey(java.awt.event.KeyEvent e)
SearchableisFindNextKey in class Searchablee - the key eventprotected boolean isFindPreviousKey(java.awt.event.KeyEvent e)
SearchableisFindPreviousKey in class Searchablee - the key eventpublic void tableChanged(javax.swing.event.TableModelEvent e)
tableChanged in interface javax.swing.event.TableModelListenerpublic void propertyChange(java.beans.PropertyChangeEvent evt)
propertyChange in interface java.beans.PropertyChangeListenerprotected boolean isActivateKey(java.awt.event.KeyEvent e)
SearchableisActivateKey in class Searchablee - the key eventprotected boolean isSelectedCellEditable()