I have a tableview with SelectionMode.MULTIPLE:
table.setEditable(true);
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
table.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {
table.getSelectionModel().getSelectedItems().forEach(System.out::println);
});
Selecting rows works finde, but if a deselect a row again (by clicking ctrl+left-click) the listener doesn't react to that immediately. What I have to do to deselect a row is the following:
According to the javadocs the selectedItem
property in multiple selection mode refers to the last item selected:
The selected item property is most commonly used when the selection model is set to be single selection, but is equally applicable when in multiple selection mode. When in this mode, the selected item will always represent the last selection made.
In your scenario, if you select "Rob", "Peter", "Max" and "John", in that order, then the selected item ends up as the last person selected ("John"), and the selected items list contains all four items. When you deselect "Peter", the last item selected is still "John". Since the selectedItem
hasn't changed, your change listener is not invoked. When you deselect another item and then reselect it, the last selected item changes to that item, and your listener is invoked.
The "weird" border you see is simply the table's display for a cell with focus (you just clicked on it) which is not selected.
To see all the changes to the selected items, you need to register a ListChangeListener
with the list of selected items:
table.getSelectionModel().getSelectedItems().addListener((Change<? extends Person> change) ->
System.out.println(table.getSelectionModel().getSelectedItems());
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