Event Filters

JavaFX Event Filters with JavaFX Overview, Install Java, Install Eclipse, JavaFX with Eclipse, JavaFX Architecture, JavaFX Application Structure, First JavaFX Application etc.

Event Filters

Event Filters

Event Filters provide the way to handle the events generated by 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 type.

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, the first argument is a 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.

  1. node.addEventFilter (<Event_Type>, new EventHandler<Event-Type>(){   
  2. public void handle(Event-Type){   
  3. //Actual logic   
  4. });   

An Event Filter can be defined for more than one Event types.

Example

The following Example illustrates the working of using the Event Filter for a node.

  1. package application;  
  2. import javafx.application.Application;  
  3. import javafx.event.EventHandler;  
  4. import javafx.scene.Group;  
  5. import javafx.scene.Scene;  
  6. import javafx.scene.control.Label;  
  7. import javafx.scene.control.TextField;  
  8. import javafx.scene.input.KeyEvent;  
  9. import javafx.scene.paint.Color;  
  10. import javafx.stage.Stage;  
  11. public class JavaFX_EventFilter extends Application{  
  12.   
  13.     @Override  
  14.     public void start(Stage primaryStage) throws Exception {  
  15.         // TODO Auto-generated method stub  
  16.           
  17.         //Adding Labels and TextFileds to the scene   
  18.         Label l1 = new Label("Press Any Key ");  
  19.         Label l2 = new Label("Filtering Event ");  
  20.           
  21.         l1.setTranslateX(100);  
  22.         l1.setTranslateY(100);  
  23.           
  24.         l2.setTranslateX(100);  
  25.         l2.setTranslateY(150);  
  26.           
  27.         TextField tf1 = new TextField();  
  28.         TextField tf2 = new TextField();  
  29.           
  30.         tf1.setTranslateX(250);  
  31.         tf1.setTranslateY(100);  
  32.         tf2.setTranslateX(250);  
  33.         tf2.setTranslateY(150);  
  34.           
  35.           
  36.         //Creating EventHandler Object   
  37.         EventHandler<KeyEvent> filter = new EventHandler<KeyEvent>() {  
  38.             @Override  
  39.             public void handle(KeyEvent event) {  
  40.                 // TODO Auto-generated method stub  
  41.                 tf2.setText("Event : "+event.getEventType());  
  42.                 tf1.setText(event.getText());  
  43.                 event.consume();  
  44.             }  
  45.         };  
  46.           
  47.         //Registering Event Filter for the event generated on text field   
  48.         tf1.addEventFilter(KeyEvent.ANY,filter );  
  49.           
  50.         //Setting Group and Scene   
  51.         Group root = new Group();  
  52.         root.getChildren().addAll(l1,l2,tf1,tf2);  
  53.         Scene scene = new Scene(root,500,300,Color.WHEAT);  
  54.         primaryStage.setScene(scene);  
  55.         primaryStage.setTitle("Adding Event Filter");  
  56.         primaryStage.show();  
  57.     }  
  58.     public static void main(String[] args) {  
  59.         launch(args);  
  60.     }  
  61.   
  62. }