Event Handlers

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

Event Handlers

Event Handlers

JavaFX facilitates us to use the Event Handlers to handle the events generated by Keyboard Actions, Mouse Actions, and many more source nodes.

Event Handlers are used to handle the events in the Event bubbling phase. There can be more than one Event handlers for a single node.

We can also use single handler for more than one node and more than one event type. In this part of the tutorial, we will discuss, how the Event Handlers can be used for processing events.

Adding an Event Handler

Event Handler must be registered for a node in order to process the events in the event bubbling phase. Event handler is the implementation of the EventHandler interface. The handle() method of the interface contains the logic which is executed when the event is triggered.

To register the EventHandler, addEventHandler() is used. In this method, two arguments are passed. One is event type and the other is EventHandler object.

The syntax of addEventHandler() is given below.

  1. node.addEventHandler(<EventType>,new EventHandler<Event-Type>()  
  2. {  
  3. public void handle(<EventType> e)  
  4. {  
  5. //handling code   
  6. }  
  7. });  


In the following example, same event handler is registered with two different buttons. The event source is discriminated in the handle() method. The circle starts translating in the positive X direction when the Play button is clicked while It gets paused when the Pause button is clicked.

  1. package application;  
  2. import javafx.animation.TranslateTransition;  
  3. import javafx.application.Application;  
  4. import javafx.event.EventHandler;  
  5. import javafx.scene.Group;  
  6. import javafx.scene.Scene;  
  7. import javafx.scene.control.Button;  
  8. import javafx.scene.input.MouseEvent;  
  9. import javafx.scene.paint.Color;  
  10. import javafx.scene.shape.Circle;  
  11. import javafx.stage.Stage;  
  12. import javafx.util.Duration;  
  13. public class JavaFX_EventHandler extends Application{  
  14. @Override  
  15. public void start(Stage primaryStage) throws Exception {  
  16.     // TODO Auto-generated method stub  
  17.     //Creating Circle and setting the color and stroke in the circle   
  18.     Circle c = new Circle(100,100,50);  
  19.     c.setFill(Color.GREEN);  
  20.     c.setStroke(Color.BLACK);  
  22.     //creating play button and setting coordinates for the button   
  23.     Button btn = new Button("Play");  
  24.     btn.setTranslateX(125);  
  25.     btn.setTranslateY(200);  
  27.     // creating pause button and setting coordinate for the pause button   
  28.     Button btn1 = new Button("Pause");  
  29.     btn1.setTranslateX(175);  
  30.     btn1.setTranslateY(200);  
  32.     //Instantiating TranslateTransition class to create the animation   
  33.     TranslateTransition trans = new TranslateTransition();  
  35.     //setting attributes for the TranslateTransition   
  36.     trans.setAutoReverse(true);  
  37.     trans.setByX(200);  
  38.     trans.setCycleCount(100);  
  39.     trans.setDuration(Duration.millis(500));  
  40.     trans.setNode(c);  
  42.     //Creating EventHandler   
  43.     EventHandler<MouseEvent> handler = new EventHandler<MouseEvent>() {  
  45.         @Override  
  46.         public void handle(MouseEvent event) {  
  47.             // TODO Auto-generated method stub  
  49.             if(event.getSource()==btn)  
  50.             {  
  51.             trans.play(); //animation will be played when the play button is clicked   
  52.             }  
  53.             if(event.getSource()==btn1)  
  54.             {  
  55.                 trans.pause(); //animation will be paused when the pause button is clicked   
  56.             }  
  57.             event.consume();  
  58.         }  
  60.     };  
  62.     //Adding Handler for the play and pause button   
  63.     btn.setOnMouseClicked(handler);  
  64.     btn1.setOnMouseClicked(handler);  
  66.     //Creating Group and scene   
  67.     Group root = new Group();  
  68.     root.getChildren().addAll(c,btn,btn1);  
  69.     Scene scene = new Scene(root,420,300,Color.WHEAT);  
  70.     primaryStage.setScene(scene);  
  71.     primaryStage.setTitle("EventHandler example");  
  72.     primaryStage.show();  
  73. }  
  74. public static void main(String[] args) {  
  75.     launch(args);  
  76. }  

Removing EventHandler

when we no longer need an EventHandler to process the events for a node or event types, we can remove the EventHandler by using the method removeEventHandler() method. This method takes two arguments, event type and EventHandler Object.

  1. node.removeEventHandler(<EventType>,handler);