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
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.
- node.addEventHandler(<EventType>,new EventHandler<Event-Type>()
- {
- public void handle(<EventType> e)
- {
- //handling code
- }
- });
Example
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.
- package application;
- import javafx.animation.TranslateTransition;
- import javafx.application.Application;
- import javafx.event.EventHandler;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- public class JavaFX_EventHandler extends Application{
- @Override
- public void start(Stage primaryStage) throws Exception {
- // TODO Auto-generated method stub
- //Creating Circle and setting the color and stroke in the circle
- Circle c = new Circle(100,100,50);
- c.setFill(Color.GREEN);
- c.setStroke(Color.BLACK);
- //creating play button and setting coordinates for the button
- Button btn = new Button("Play");
- btn.setTranslateX(125);
- btn.setTranslateY(200);
- // creating pause button and setting coordinate for the pause button
- Button btn1 = new Button("Pause");
- btn1.setTranslateX(175);
- btn1.setTranslateY(200);
- //Instantiating TranslateTransition class to create the animation
- TranslateTransition trans = new TranslateTransition();
- //setting attributes for the TranslateTransition
- trans.setAutoReverse(true);
- trans.setByX(200);
- trans.setCycleCount(100);
- trans.setDuration(Duration.millis(500));
- trans.setNode(c);
- //Creating EventHandler
- EventHandler<MouseEvent> handler = new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent event) {
- // TODO Auto-generated method stub
- if(event.getSource()==btn)
- {
- trans.play(); //animation will be played when the play button is clicked
- }
- if(event.getSource()==btn1)
- {
- trans.pause(); //animation will be paused when the pause button is clicked
- }
- event.consume();
- }
- };
- //Adding Handler for the play and pause button
- btn.setOnMouseClicked(handler);
- btn1.setOnMouseClicked(handler);
- //Creating Group and scene
- Group root = new Group();
- root.getChildren().addAll(c,btn,btn1);
- Scene scene = new Scene(root,420,300,Color.WHEAT);
- primaryStage.setScene(scene);
- primaryStage.setTitle("EventHandler example");
- primaryStage.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
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.
- node.removeEventHandler(<EventType>,handler);