How to intercept MouseEvent.MOUSE_CLICKED on TableView on JavaFX8

一世执手 提交于 2020-01-06 13:29:36

问题


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.

Unfortunately - my EventFilter seems to fire after the sort was completed.

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

But by the time my EventFilter fires, the table has been sorted.

Any thoughts?


回答1:


Use MouseEvent.MOUSE_PRESSED

MouseEvent.MOUSE_CLICKED is fired after MouseEvent.MOUSE_RELEASED that's too late to intercept listeners and listeners :)



来源:https://stackoverflow.com/questions/37392849/how-to-intercept-mouseevent-mouse-clicked-on-tableview-on-javafx8

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