How to cancel selection in ListView?

有些话、适合烂在心里 提交于 2020-01-05 07:33:52

问题


I try to cancel user selection in a ListView according a condition. I tried to consume mouse clicked and mouse pressed event in ListView and ListCell but it didn't work. I don't understand why bu events occur after the selected item property change. How can I cancel the user selection?


回答1:


If I understand correctly, you want to prevent users from selecting items by clicking on it. I further suppose that you tried a solution like this:

listView.addEventHandler(MOUSE_CLICKED, click -> click.consume());

That does not prevent other click handlers from being executed. Even if it did, the internal event handler seems to be fired before your event handler, since the selected item is changed before your handler is executed.

You need to add an event filter to prevent any event handlers from being fired:

listView.addEventFilter(MOUSE_CLICKED, click -> click.consume());



回答2:


    listView.getSelectionModel().clearSelection();


来源:https://stackoverflow.com/questions/26871891/how-to-cancel-selection-in-listview

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