用于JavaFx列表视图中的多个选择的事件侦听器 [英] Event listener for multiple selection in a JavaFx listview

查看:75
本文介绍了用于JavaFx列表视图中的多个选择的事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个JavaFX listview,可以选择多个项目.我已经弄清楚了选择一个项目时需要使用哪个事件侦听器,但是当我取消选择一个项目时并不总是会触发此侦听器. 所以我的问题是,是否存在用于选择和取消选择项目的事件侦听器?

I have a JavaFX listview in my code and multiple items can be selected. I have already figured out which event listener I need to use when an item is selected but this listener isn't always triggered when i deselect an item. So my question is, is there an event listener for both selecting and deselecting items?

这是我当前正在使用的事件侦听器:

This is the event listener I'm currently using:

lvLijst.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue)
        {
            //code
        }
    });

谢谢.

推荐答案

您需要收听选定项目的列表,而不是单个选定项目.启用多项选择后,selectedItemProperty()将始终引用选择了多个项目时选择的最后一个(及时)项目.列表更改时,此属性不会总是更改-特别是如果您取消选择除最后一个所选项目以外的任何项目,因此列表的每次更改都不会通知您的侦听器.

You need to listen to the list of selected items, not the single selected item. When you have multiple selection enabled, the selectedItemProperty() will always refer to the last (in time) item selected when multiple items are selected. This property will not always change when the list changes - specifically if you deselect any item other than the last one selected, so your listener will not be notified for every change to the list.

相反,做

lvLijst.getSelectionModel().getSelectedItems().addListener((Change<? extends String> c) -> {
    // code ...
});

这篇关于用于JavaFx列表视图中的多个选择的事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆