I have a TableView
with some rows. The user can select any row but when he clicks on empty rows or anywhere on the Stage
, I want to clear his current selection of the TableView
.
To select a row with a specific index you can use the select(int) method. Here is an example of selecting a single row with a given index in a JavaFX TableView: selectionModel. select(1);
The TableView class provides built-in capabilities to sort data in columns. Users can alter the order of data by clicking column headers. The first click enables the ascending sorting order, the second click enables descending sorting order, and the third click disables sorting. By default, no sorting is applied.
You can add a event filter to the Scene
that uses the selection model of the TableView
to clear the selection, if the click was on a empty row or anywhere outside of a TableView
:
scene.addEventFilter(MouseEvent.MOUSE_CLICKED, evt -> {
Node source = evt.getPickResult().getIntersectedNode();
// move up through the node hierarchy until a TableRow or scene root is found
while (source != null && !(source instanceof TableRow)) {
source = source.getParent();
}
// clear selection on click anywhere but on a filled row
if (source == null || (source instanceof TableRow && ((TableRow) source).isEmpty())) {
tableView.getSelectionModel().clearSelection();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With