Handle mouse event anywhere with JavaFX

You can add an event filter to the scene with addEventFilter(). This will be called before the event is consumed by any child controls. Here’s what the code for the event filter looks like.

scene.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent mouseEvent) {
        System.out.println("mouse click detected! " + mouseEvent.getSource());
    }
});

Leave a Comment