Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: what is the difference between EventHandler and EventFilter?

Tags:

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:

  1. EventFilter is executed before EventHandler
  2. 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 EventFilters 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!

like image 754
bog Avatar asked Sep 09 '14 08:09

bog


People also ask

What is the difference between an EventFilter and a handler?

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.

What is an event handler in JavaFX?

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.

What is the difference between an event handler and an event listener?

The listener listens out for the event happening, and the handler is the code that is run in response to it happening.

What is event filter JavaFX?

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.


2 Answers

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
  • Route construction
  • Event capturing <- filters are triggered here
  • Event bubbling <- handlers are triggered here

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.

like image 196
sirolf2009 Avatar answered Sep 21 '22 15:09

sirolf2009


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

like image 22
Ganesh Avatar answered Sep 18 '22 15:09

Ganesh