Event listener for multiple selection in a JavaFx listview

*爱你&永不变心* 提交于 2020-05-01 04:21:24

问题


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
        }
    });

Thanks in advance.


回答1:


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.

Instead, do

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


来源:https://stackoverflow.com/questions/42303663/event-listener-for-multiple-selection-in-a-javafx-listview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!