What is the meaning of event consumer in JavaFX?
I tried to understand event handling in JavaFX, where I found this line
Can you explain the meaning of event consumption?
Solution
Events travel along a specific route In most cases (such as mouse / key events), the path will start from the root node of the scene and contain each node on the path from the root node to the target node in the scene graph On the route to the target node, event filters are executed. If any of these filters should use the event, any further processing of the event will be stopped Once the event reaches the target node, any event handler is called if the "travel" returns to the root Event processing can also be stopped there by using events
Example:
@Override public void start(Stage primaryStage) { Rectangle rect = new Rectangle(50,50); StackPane root = new StackPane(rect); rect.addEventFilter(MouseEvent.MOUSE_CLICKED,evt -> { System.out.println("rect click(filter)"); // evt.consume(); }); root.addEventFilter(MouseEvent.MOUSE_CLICKED,evt -> { System.out.println("root click(filter)"); // evt.consume(); }); root.setOnMouseClicked(evt -> { System.out.println("root click(handler)"); // evt.consume(); }); rect.setOnMouseClicked(evt -> { System.out.println("rect click(handler)"); // evt.consume(); }); Scene scene = new Scene(root,200,200); primaryStage.setScene(scene); primaryStage.show(); }
If you click rect, event processing starts at the root node The filter is executed here If the event is not used in the filter, it is passed to the rect node where the event filter receives the event If the filter does not use the event, the event handler of rect will receive the event If the event is not eliminated by the event handler, the event handler of the root node will receive the event
Just uncomment some EVT Call consume () to see what happens