I've been googling for a while and I found that basicly, some web pages says there are no big differences. Except for some points:
EventFilter
is executed before EventHandler
EventFilter
is not sensible to event.Consume();
Let me see If I've understood it: Lets say I have:
Button b= new Button("Test"); b.addEventHandler(.....){...}; b.addEventFilter(......){...};
Let's say they are both "linked" to an MouseEvent.MOUSE_CLICKED
; then, EventFilter
's code will be the first to be executed!?
Let's say, now, I have:
Button b= new Button("Test"); b.addEventHandler(.....); b.addEventFilter(......){ //some code event.consume(); }; // First filter b.addEventFilter(......){ //some other code event.consume(); }; // Second filter
In this case, boath EventFilter
s will be executed but the EventHandler
will not. Right?
Are there any other things to know? Are there situations where I should prefere one or other? should I sometimes use them together in order to solve some problems?
Thank you!
An event filter is an implementation of the EventHandler interface. The handle() method of this interface provides the code that is executed when the event that is associated with the filter is received by the node that registered the filter. To register a filter, use the addEventFilter() method.
In JavaFX, Event Handling is a process which controls an event as well as decides what has to be triggered, when an event occurs. This will be done writing a code called as an event handler that executes when an event occurs. For single node, it can be more than one event handlers.
The listener listens out for the event happening, and the handler is the code that is run in response to it happening.
EventFilter enable you to handle an event during the event capturing phase but the event handler handle events during the event bubbling phase. So EventFilter is executed before the EventHandler. java docs.
Whenever an event happens, it follows a process to determine which node in the scene graph should handle the event. The process takes these steps:
Target Selection Say that your scene contains a pane with a circle. If you click on the circle, the circle becomes the event target.
Route Construction Next, JavaFX creates a route (or an event dispatch chain). In our example the chain would look like stage -> scene -> pane -> circle
Event Capturing The event gets carried through every event filter on the chain. As soon as one of the filters calls consume()
, the chain stops and that node becomes the target. If no filter calls consume()
the end of the chain (the circle) remains the target.
Event Bubbling Next, the event get pushed through the chain again, but this time from the event target to the stage. So if the pane event filter called consume()
, the following event handlers will be hit: pane -> scene -> stage
So the difference is not only when these handlers get activated, but also that event filters can prevent child nodes from receiving events.
As I know JavaFX EventFilter can be one or many for a single node and can be single for many nodes. EventFilter enable you to handle an event during the event capturing phase but the event handler handle events during the event bubbling phase.
So EventFilter is executed before the EventHandler.
java docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With