[英]How to listen to deselection of row in JavaFX tableview?
I have a tableview with SelectionMode.MULTIPLE: 我有SelectionMode.MULTIPLE的tableview:
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. 选择行有效,但如果再次取消选择一行(通过单击ctrl +左键单击),则侦听器不会立即对此作出反应。 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: 根据javadocs ,多选模式中的
selectedItem
属性指的是所选的最后一项:
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. 在您的方案中,如果您按顺序选择“Rob”,“Peter”,“Max”和“John”,则所选项目最终将作为最后一个人选择(“John”),并且所选项目列表包含所有四个项目。 When you deselect "Peter", the last item selected is still "John".
取消选择“Peter”时,所选的最后一项仍然是“John”。 Since the
selectedItem
hasn't changed, your change listener is not invoked. 由于
selectedItem
未更改,因此不会调用更改侦听器。 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: 要查看所选项目的所有更改,您需要使用所选项目列表注册
ListChangeListener
:
table.getSelectionModel().getSelectedItems().addListener((Change<? extends Person> change) ->
System.out.println(table.getSelectionModel().getSelectedItems());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.