Convenience Methods

JavaFX Convenience methods with JavaFX Overview, Install Java, Install Eclipse, JavaFX with Eclipse, JavaFX Architecture, JavaFX Application Structure, First JavaFX Application etc.

Convenience Methods

Convenience Methods

JavaFX provides the convenience methods which can be used to handle events within our JavaFX application. They provide an easy way to create and register event handlers to respond to KeyEvent, MouseEvent, Action Event, Drag & Drop Events and many more.

Node class contains various Event Handler properties which can be set to the User defined Event Handlers using the setter methods defined in the class.

Setting, EventHandler properties of the Node class, to the user defined event handlers, automatically registers the handlers to receive the corresponding event types.

The EventHandler properties along with their setter methods (convenience methods) are described in the following table.

EventHandler Property Description Setter Methods
onContextMenuRequested This is of the type EventHandler of ContextMenuEvent. This is assigned to a function which is to be called when the context menu is requested on the node. setOnContextMenuRequested(EventHandler value )
onDragDetected This is of the type EventHandler of MouseEvent. This indicates a function which is to be called when the drag gesture is detected. setOnDragDetected(EventHandler value )
onDragDone This is of the type EventHandler of DragEvent. setOnDragDone(EventHandler value )
onDragDropped This is of the type EventHandler of DragEvent. This is assigned to a function which is to be called when the mouse is released during a drag and drop operation. setOnDragDropped(EventHandler value )
onDragEntered This is of the type EventHandler of DragEvent. This is assigned to a function which is to be called when the drag gesture enters the node. setOnDragEntered(EventHandler value )
onDragExited This is of the type EventHandler of DragEvent. This is assigned to a function which is to be called when the drag gesture exits the node. setOnDragExited(EventHandler value )
onDragOver This is of the type EventHandler of DragEvent.This is assigned to a function which is to be called when the drag gesture progresses within the node. setOnDragOver(EventHandler value )
onInputMethodTextChanged This is of the type EventHandler of InputMethodEvent. This is assigned to a function which is to be called when the Node has focus and the input method text has changed. setOnInputMethodTextChanged(EventHandler value )
onKeyPressed This is of the type EventHandler of KeyEvent. This is assigned to a function which is to be called when the Node has focus and key is pressed. setOnKeyPressed(EventHandler value )
onKeyReleased This is of the type EventHandler of KeyEvent. This is assigned to a function which is to be called when the Node has focus and key is released. setOnKeyReleased(EventHandler value )
onKeyTyped This is of the type EventHandler of KeyEvent.This is assigned to a function which is to be called when the Node has focus and key is typed. setOnKeyTyped(EventHandler value )
onMouseClicked This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse button is clicked on the node. setOnMouseClicked(EventHandler value )
onMouseDragEntered This is of the type EventHandler of MouseDragEvent. This is assigned to a function which is to be called when a full press drag release gesture enters the node. setOnMouseDragEntered(EventHandler value )
onMouseDragExited This is of the type EventHandler of MouseDragEvent.This is assigned to a function which is to be called when a full press drag release gesture exits the node. setOnMouseDragExited(EventHandler value )
onMouseDragged This is of the type EventHandler of MouseDragEvent. This is assigned to a function which is to be called when the mouse button is pressed and dragged on the node. setOnMouseDragged(EventHandler value )
onMouseDragOver This is of the type EventHandler of MouseDragEvent. This is assigned to a function which is to be called when a full press drag release gesture progresses within the node. setOnMouseDragOver(EventHandler value )
onMouseDragReleased This is of the type EventHandler of MouseDragEvent. This is assigned to a function which is to be called when a full press drag release gesture ends within the node. setOnMouseDragReleased(EventHandler value )
onMouseEntered This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse enters the node. setOnMouseEntered(EventHandler value )
onMouseExited This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse exits the node. setOnMouseExited(EventHandler value )
onMouseMoved This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse moves within the node and no button has been pushed. setOnMouseMoved(EventHandler value )
onMousePressed This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse button is pressed on the node. setOnMousePressed(EventHandler value )
onMouseReleased This is of the type EventHandler of MouseEvent. This is assigned to a function which is to be called when the mouse button is released on the node. setOnMouseReleased(EventHandler value )
onRotate This is of the type EventHandler of RotateEvent. This is assigned to a function which is to be called when the rotation action is performed on the node. setOnRotate(EventHandler value )
onRotationFinished This is of the type EventHandler of RotateEvent. This is assigned to a function which is to be called when the rotation gesture ends. setOnRotationFinished(EventHandler value )
onRotationStarted This is of the type EventHandler of RotateEvent. This is assigned to a function which is to be called when the rotation gesture is first detected. setOnRotationStarted(EventHandler value )
onScrollFinished This is of the type EventHandler of ScrollEvent. This is assigned to a function which is to be called when the scroll gesture ends. setOnScrollFinished(EventHandler value )
onScroll This is of the type EventHandler of ScrollEvent. This is assigned to a function which is to be called when the scroll action is performed. setOnScroll(EventHandler value )
onScrollStarted This is of the type EventHandler of ScrollEvent. This is assigned to a function which is to be called when the scrolling gesture is detected. setOnScrollStarted(EventHandler value )
onSwipeDown This is of the type EventHandler of SwipeEvent. This is assigned to a function which is to be called when the downwards swipe happens on the node. setOnSwipeDown(EventHandler value )
onSwipeUP This is of the type EventHandler of SwipeEvent. This is assigned to a function which is to be called when the upwards swipe happens on the node. setOnSwipeUP(EventHandler value )
onSwipeLeft This is of the type EventHandler of SwipeEvent. This is assigned to a function which is to be called when the leftwards swipe happens on the node. setOnSwipeLeft(EventHandler value )
onSwipeRight This is of the type EventHandler of SwipeEvent. This is assigned to a function which is to be called when the Rightwards swipe happens on the node. setOnSwipeRight(EventHandler value )
onTouchMoved This is of the type EventHandler of TouchEvent. This is assigned to a function which is to be called when the touch point is moved on the node. setOnTouchMoved(EventHandler value )
onTouchReleased This is of the type EventHandler of TouchEvent. This is assigned to a function which is to be called when the touch point is released on the node. setOnTouchReleased(EventHandler value )
onTouchStationary This is of the type EventHandler of TouchEvent. This is assigned to a function which is to be called when the touch point is pressed and stays still setOnTouchStationary(EventHandler value )
onZoomFinished This is of the type EventHandler of ZoomEvent. This is assigned to a function which is to be called when the zooming gesture ends. setOnZoomFinished(EventHandler value )
onZoom This is of the type EventHandler of ZoomEvent. This is assigned to a function which is to be called when the zooming gesture is performed. setOnZoom(EventHandler value )
onZoomStarted This is of the type EventHandler of ZoomEvent. This is assigned to a function which is to be called when the zooming gesture is detected. setOnZoomStarted(EventHandler value )

Convenience methods for the event handlers registration has the format like

  1. setOnEvent-type(EventHandler<? super event-class> value)  

where the Event type is the type of the event that is to be handled through the defined functions, for example, setOnMouseMoved() will be the convenience method to register the event handler for the event Mouse_Moved.

setOnAction() Example for a Button Action

In the following example, setOnAction() method is illustrated. The EventHandler registered with the setOnAction() method is called when the Play button is clicked and it is set to rotate the rectangle on the screen.

The pause button is also registered with the EventHandler which is set to stop the rotation of the rectangle.

  1. package application;  
  2. import javafx.animation.RotateTransition;  
  3. import javafx.application.Application;  
  4. import javafx.event.ActionEvent;  
  5. import javafx.event.EventHandler;  
  6. import javafx.scene.Group;  
  7. import javafx.scene.Scene;  
  8. import javafx.scene.control.Button;  
  9. import javafx.scene.paint.Color;  
  10. import javafx.scene.shape.Rectangle;  
  11. import javafx.stage.Stage;  
  12. import javafx.util.Duration;  
  13.   
  14. public class EventHandlerExample extends Application {  
  15.   public static void main(String[] args) {  
  16.     Application.launch(args);  
  17.   }  
  18.   
  19.   @Override  
  20.   public void start(Stage primaryStage) {  
  21.       
  22.     // Creating Rectangle   
  23.     Rectangle rect = new Rectangle(100,100,120,120);  
  24.       
  25.     // Setting Stroke and colour for the rectangle   
  26.     rect.setFill(Color.RED);  
  27.     rect.setStroke(Color.BLACK);  
  28.       
  29.     // Instantiating RotateTransition class   
  30.     RotateTransition rotate = new RotateTransition();  
  31.       
  32.     //Setting properties for the Rotate Transition class   
  33.     rotate.setAutoReverse(false);  
  34.     rotate.setByAngle(360);  
  35.     rotate.setCycleCount(500);  
  36.     rotate.setDuration(Duration.millis(500));  
  37.     rotate.setNode(rect);  
  38.       
  39.     //Creating the play button   
  40.     Button btn = new Button();  
  41.       
  42.     //Setting properties for the play button   
  43.     btn.setText("Play");  
  44.     btn.setTranslateX(100);  
  45.     btn.setTranslateY(250);  
  46.       
  47.     //defining the convenience method to register the event handler to handle the Action event.      
  48.     btn.setOnAction(new EventHandler<ActionEvent>() {  
  49.       public void handle(ActionEvent event) {  
  50.             
  51.           rotate.play();  
  52.       }  
  53.     });  
  54.       
  55.     //Creating the pause button   
  56.     Button btn1 = new Button("Pause");  
  57.       
  58.     //Setting propertied for the pause button   
  59.     btn1.setTranslateX(160);  
  60.     btn1.setTranslateY(250);  
  61.       
  62.     //Handling event for the pause button click event   
  63.     btn1.setOnAction(new EventHandler<ActionEvent>() {  
  64.   
  65.         @Override  
  66.         public void handle(ActionEvent arg0) {  
  67.             // TODO Auto-generated method stub  
  68.             rotate.pause();  
  69.         }  
  70.           
  71.     });  
  72.       
  73.     //Configuring group and scene   
  74.     Group root = new Group();  
  75.     Scene scene = new Scene(root, 400350);  
  76.     root.getChildren().addAll(btn,rect,btn1);  
  77.     primaryStage.setScene(scene);  
  78.     primaryStage.setTitle("Handling Events");  
  79.     primaryStage.show();  
  80.   }  
  81. }  

setOnKeyEvent() Example for a Key Event

The setOnKeyEvent() method can be used for registering the Event Handler logic for the key event generated on a node. In the following example, two text fields are created as the node, the key pressed in the first text field is set as the text in the second text field.

  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.TextField;  
  7. import javafx.scene.input.KeyEvent;  
  8. import javafx.scene.paint.Color;  
  9. import javafx.stage.Stage;  
  10. public class JavaFX_KeyEvent extends Application{  
  11.   
  12.     @Override  
  13.     public void start(Stage primaryStage) throws Exception {  
  14.           
  15.         // TODO Auto-generated method stub  
  16.           
  17.         //Creating TextFields and setting position for them   
  18.         TextField tf1 = new TextField();  
  19.         TextField tf2 = new TextField();  
  20.         tf1.setTranslateX(100);  
  21.         tf1.setTranslateY(100);  
  22.         tf2.setTranslateX(300);  
  23.         tf2.setTranslateY(100);  
  24.           
  25.         //Handling KeyEvent for textfield 1   
  26.         tf1.setOnKeyPressed(new EventHandler<KeyEvent>() {  
  27.   
  28.             @Override  
  29.             public void handle(KeyEvent key) {  
  30.                 // TODO Auto-generated method stub  
  31.                 tf2.setText("Key Pressed :"+" "+key.getText());  
  32.             }  
  33.               
  34.         });  
  35.           
  36.         //setting group and scene   
  37.         Group root = new Group();  
  38.         root.getChildren().addAll(tf2,tf1);  
  39.         Scene scene = new Scene(root,500,200,Color.WHEAT);  
  40.         primaryStage.setScene(scene);  
  41.         primaryStage.setTitle("Handling KeyEvent");  
  42.         primaryStage.show();  
  43.     }  
  44.     public static void main(String[] args) {  
  45.         launch(args);  
  46.     }  
  47.   
  48.