In Javafx, does mouseReleased event always happen before mouseClicked?

Viewed 1106

Which event is fired first? Does it depend on initialization order? Does it depend on something else? What is the principle behind this?

pane.setOnMouseClicked(e -> {
    doSomething();
});

pane.setOnMouseReleased(e -> {
    doSomething();
});
1 Answers

You are actually adding an event handler to your Node, which handles a specific type of a MouseEvent. ( Have a look at Handling JavaFX Events as well )

If you take a look at MouseEvent documentation you will see :

MOUSE_PRESSED

public static final EventType<MouseEvent> MOUSE_PRESSED

This event occurs when mouse button is pressed. This activates a press-drag-release gesture, so all subsequent mouse events until the button is released are delivered to the same node.

MOUSE_RELEASED

public static final EventType<MouseEvent> MOUSE_RELEASED

This event occurs when mouse button is released. It is delivered to the same node where the button has been pressed which activated a press-drag-release gesture.

MOUSE_CLICKED

public static final EventType<MouseEvent> MOUSE_CLICKED

This event occurs when mouse button has been clicked (pressed and released on the same node). This event provides a button-like behavior to any node. Note that even long drags can generate click event (it is delivered to the top-most node on which the mouse was both pressed and released).

So to answer your question yes, the order of events is always :

MOUSE_PRESSED ->  MOUSE_RELEASED -> MOUSE_CLICKED
Related