How to intercept MouseEvent.MOUSE_CLICKED on Table

2019-09-14 19:34发布

问题:

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 :)