public class TableViewSearchable<T> extends Searchable<java.lang.Object>
TableSearchable is an concrete implementation of Searchable that enables the search function in
TableView. It's very simple to use it. Assuming you have a TableView, all you need to do is to call
TableView table = ....;
TableSearchable searchable = new TableSearchable(table);
Now the TableView will have the search function.
As TableView is a two dimension data, the search is a little different from ListView and TreeView 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 TableView 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 TableView 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.
TableView table = ....;
TableViewSearchable searchable = new TableViewSearchable(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,
caeSensitiveProperty, fromStartProperty, matchingElementProperty, matchingIndexProperty, popupPositionProperty, popupPositionRelativeToProperty, repeatsProperty, searchingDelayProperty, searchingLabelProperty, searchingProperty, searchingTextProperty, typedTextProperty, wildcardEnabledPropertySearchable.SearchPopup_boundsListener, _keyListener, _node, _visibleListener, PROPERTY_SEARCHABLE| Constructor and Description |
|---|
TableViewSearchable(javafx.scene.control.TableView<T> tableView) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addTableSelection(javafx.scene.control.TableView<T> 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 control.
|
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(javafx.scene.control.TableView table,
int rowIndex,
int columnIndex)
Get string value of the table.
|
void |
installListeners()
Installs necessary listeners to the control.
|
protected boolean |
isActivateKey(javafx.scene.input.KeyEvent e)
Checks if the key in KeyEvent should activate the search popup.
|
protected boolean |
isFindNextKey(javafx.scene.input.KeyEvent e)
Checks if the key is used as a key to find the next occurrence.
|
protected boolean |
isFindPreviousKey(javafx.scene.input.KeyEvent e)
Checks if the key is used as a key to find the previous occurrence.
|
protected boolean |
isRowSelectionAllowed(javafx.scene.control.TableView<T> 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 |
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 |
uninstallListeners()
Uninstall the listeners that installed before.
|
adjustSelectedIndex, caeSensitiveProperty, compare, compareAsString, createSearchPopup, findAll, findFirst, findFromCursor, findLast, findNext, findPrevious, fromStartProperty, getCurrentIndex, getCursor, getElementAtAsString, getHidePopupDelay, getMatchingElement, getMatchingIndex, getNode, getPopupPosition, getPopupPositionRelativeTo, getResourceString, getSearchable, getSearchingDelay, getSearchingText, getSearchLabel, getWildcardSupport, hidePopup, highlightAll, isCaseSensitive, isDeactivateKey, isFindFirstKey, isFindLastKey, isFromStart, isIncrementalSelectKey, isNavigationKey, isPopupVisible, isRepeats, isReverseOrder, isSearching, isSelectAllKey, isWildcardEnabled, keyTypedOrPressed, matchingElementProperty, matchingIndexProperty, popupPositionProperty, popupPositionRelativeToProperty, repeatsProperty, reverseFindFromCursor, searchingDelayProperty, searchingLabelProperty, searchingProperty, searchingTextProperty, select, setCaseSensitive, setCursor, setCursor, setFromStart, setHidePopupDelay, setMatchingElement, setMatchingIndex, setPopupPosition, setPopupPositionRelativeTo, setRepeats, setReverseOrder, setSearching, setSearchingDelay, setSearchingText, setSearchLabel, setWildcardEnabled, setWildcardSupport, showPopup, typedTextProperty, wildcardEnabledPropertypublic TableViewSearchable(javafx.scene.control.TableView<T> tableView)
public void installListeners()
SearchableinstallListeners in class Searchable<java.lang.Object>public void uninstallListeners()
SearchableuninstallListeners in class Searchable<java.lang.Object>protected void setSelectedIndex(int index,
boolean incremental)
SearchablesetSelectedIndex in class Searchable<java.lang.Object>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(javafx.scene.control.TableView<T> table, int rowIndex, int columnIndex, boolean incremental)
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 isRowSelectionAllowed(javafx.scene.control.TableView<T> table)
table - the table.protected boolean isSearchSelectedRows()
protected int getSelectedIndex()
getSelectedIndex in class Searchable<java.lang.Object>protected java.lang.Object getElementAt(int index)
SearchablegetElementAt in class Searchable<java.lang.Object>index - the indexprotected java.lang.Object getValueAt(javafx.scene.control.TableView table,
int rowIndex,
int columnIndex)
table - the TableViewrowIndex - the row indexcolumnIndex - the column indexprotected int getElementCount()
SearchablegetElementCount in class Searchable<java.lang.Object>protected java.lang.String convertElementToString(java.lang.Object item)
SearchableconvertElementToString in class Searchable<java.lang.Object>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(javafx.scene.input.KeyEvent e)
SearchableisFindNextKey in class Searchable<java.lang.Object>e - the key eventprotected boolean isFindPreviousKey(javafx.scene.input.KeyEvent e)
SearchableisFindPreviousKey in class Searchable<java.lang.Object>e - the key eventprotected boolean isActivateKey(javafx.scene.input.KeyEvent e)
SearchableisActivateKey in class Searchable<java.lang.Object>e - the key eventprotected boolean isSelectedCellEditable()