简体   繁体   English

如何在JavaFX tableview中监听取消选择行?

[英]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: 我要取消选择行的方法如下:

  1. Select lets say 'Rob', 'Peter', 'Max' and 'John' (Either by selecting them individualy with ctrl+left-click or all of them together by shift+left click) 选择让我们说'Rob','Peter','Max'和'John' (或者通过ctrl +左键单击选择它们,或者通过shift + left click将它们全部组合在一起)
  2. Unselect 'Peter' by clicking ctrl+left-click (now this row has a weird blue border and the listener didn't detected the change) 通过单击ctrl +左键单击取消选择“Peter” (现在此行有一个奇怪的蓝色边框,并且侦听器未检测到更改)
  3. Deselect another row (Now this row has this weird border and 'Peter' is row looks normal) 取消选择另一行(现在这行有这个奇怪的边框,'Peter'是行看起来正常)
  4. Reselect the previous row (Now the listener detects that I unselected 'Peter' ) 重新选择上一行(现在侦听器检测到我未选中'Peter'
    在此输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM