如何在JavaFX8的TableView上拦截MouseEvent.MOUSE_CLICKED [英] How to intercept MouseEvent.MOUSE_CLICKED on TableView on JavaFX8
问题描述
我正在创建一个自定义CheckBoxTableView,其中所选项目与CheckBox一起显示.如果选择项目后用户尝试对表格进行排序,则该表格看起来很混乱.我想提示用户,看看他们是否要继续.如果是这样,我想清除选择,如果没有,请仅使用该事件,这样就不会发生排序.
I am creating a custom CheckBoxTableView where the selected items are displayed with a CheckBox. If the user attempts to sort the table once items are selected, it appears to mess up. I would like to prompt the user to see if they would like to continue. If so, I would like to clear the selection, if not, simply consume the event so the sorting doesn't happen.
不幸的是,排序完成后,我的EventFilter似乎启动了.
Unfortunately - my EventFilter seems to fire after the sort was completed.
在TableView构造函数上,我放置了以下代码:
On the TableView constructor, I placed the following code:
addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
if(event.getTarget() instanceof TableColumnHeader) {
Alert a = new Alert(Alert.AlertType.CONFIRMATION);
a.setContextText("you sure?");
Optional<ButtonType> bt = a.showAndWait();
if(bt.isPresent() && bt.get() == ButtonType.OK){
//Clear selection
getSelectionModel().clearSelection();
}else {
event.consume();
}
}
});
但是,当我的EventFilter触发时,该表已被排序.
But by the time my EventFilter fires, the table has been sorted.
有什么想法吗?
推荐答案
使用MouseEvent.MOUSE_PRESSED
MouseEvent.MOUSE_CLICKED
在MouseEvent.MOUSE_RELEASED
之后触发,为时已晚,无法拦截侦听器和侦听器:)
MouseEvent.MOUSE_CLICKED
is fired after MouseEvent.MOUSE_RELEASED
that's too late to intercept listeners and listeners :)
这篇关于如何在JavaFX8的TableView上拦截MouseEvent.MOUSE_CLICKED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!