Event Filters provides the way to handle the events generated by the keyboard actions, mouse actions, scroll actions and many more event sources.
Event Filters process the events during the Event capturing phase of event handling. Event Filters need to be registered with the node in order to provide the event handling logic for the event generated on the node. However, JavaFX enables us to register a single event filter for more than one node and more than one event types.
The Event Filter provides the implementation of EventHandler interface. The handle() method of the interface provides the code which will be executed when the event is generated.
Adding Event-Filter to a node
To register the event filter for a node, the method addEventFilter() is used. This method takes two arguments, first argument is type of the event that is generated and the second argument is the filter which will have the code to handle the event.
The syntax of adding the event filter is given below.
An Event Filter can be defined for more than one Event types.
The following Example illustrates the working of using the Event Filter for a node.
When we no longer need a Event filter to handle the event generated for a particular node to process the events, we can remove the filter by using removeEventFilter() method.
There are two arguments passed in the method, first is the Event type and second is the filter which is EventHandler class object.
Use the following syntax for removing Event-Filter registered with the node.
where, filter is the object of EventHandler class.