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.SearchField, Searchable.SearchPopup
_component, _componentListener, _focusListener, _keyListener, 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.
|
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, compare, compare, createComponentListener, createFocusListener, createKeyListener, createSearchPopup, findFirst, findFromCursor, findLast, findNext, findPrevious, firePropertyChangeEvent, fireSearchableEvent, getBackground, getComponent, getCurrentIndex, getCursor, getForeground, getMismatchForeground, getPopupLocation, getPopupLocationRelativeTo, getResourceString, getSearchable, getSearchableListeners, getSearchableProvider, getSearchingDelay, getSearchingText, getSearchLabel, getWildcardSupport, hidePopup, isCaseSensitive, isDeactivateKey, isFindFirstKey, isFindLastKey, isFromStart, isHeavyweightComponentEnabled, isIncrementalSelectKey, isNavigationKey, isPopupVisible, isProcessModelChangeEvent, isRepeats, isReverseOrder, isSelectAllKey, isWildcardEnabled, keyTypedOrPressed, removePropertyChangeListener, removeSearchableListener, reverseFindFromCursor, setBackground, setCaseSensitive, setCursor, setCursor, setForeground, setFromStart, setHeavyweightComponentEnabled, setMismatchForeground, setPopupLocation, setPopupLocationRelativeTo, setProcessModelChangeEvent, setRepeats, setReverseOrder, setSearchableProvider, setSearchingDelay, setSearchLabel, setWildcardEnabled, setWildcardSupport, showPopup
public void installListeners()
Searchable
installListeners
in class Searchable
public void uninstallListeners()
Searchable
uninstallListeners
in class Searchable
protected void setSelectedIndex(int index, boolean incremental)
Searchable
setSelectedIndex
in class Searchable
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.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 Searchable
protected java.lang.Object getElementAt(int index)
Searchable
getElementAt
in class Searchable
index
- the indexprotected int getElementCount()
Searchable
getElementCount
in class Searchable
protected java.lang.String convertElementToString(java.lang.Object item)
Searchable
convertElementToString
in class Searchable
item
- 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)
Searchable
isFindNextKey
in class Searchable
e
- the key eventprotected boolean isFindPreviousKey(java.awt.event.KeyEvent e)
Searchable
isFindPreviousKey
in class Searchable
e
- the key eventpublic void tableChanged(javax.swing.event.TableModelEvent e)
tableChanged
in interface javax.swing.event.TableModelListener
public void propertyChange(java.beans.PropertyChangeEvent evt)
propertyChange
in interface java.beans.PropertyChangeListener
protected boolean isActivateKey(java.awt.event.KeyEvent e)
Searchable
isActivateKey
in class Searchable
e
- the key eventprotected boolean isSelectedCellEditable()