JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any event handler like MouseEvent, KeyEvent, ActionEvent on different JavaFX application nodes. Setters methods are generally used to apply various properties on nodes to handle various events using event handler properties. The convenience methods helps us to automatically register handlers with particular components to receive specific event handlers.

Convenience methods in JavaFX to support Event Handling.

  1. onKeyPressed: This is KeyEvent handler type property. It is assigned by the user to particular function, when the node encounters action of key pressed.
  2. onKeyReleased: This is KeyEvent handler type property. It is assigned by the user to particular function, when the node encounters action of key released.
  3. onKeyTyped: This is KeyEvent handler type property. It is assigned by the user to particular function, when the node encounters action of key typed.
  4. onMouseClicked: This is MouseEvent handler type property. It is assigned by the user to particular function, when the node encounters action of mouse clicking.
  5. onMouseDragged: This is MouseEvent handler type property. It is assigned by the user to particular function, when the node encounters action of mouse dragging.
  6. onMouseEntered: This is MouseEvent handler type property. It is assigned by the user to particular function, when the mouse enters the scope of the node.
  7. onMouseExited: This is MouseEvent handler type property. It is assigned by the user to particular function, when the mouse exits the scope of the node.
  8. onMouseMoved: This is MouseEvent handler type property. It is assigned by the user to particular function, when the mouse moves within the node without pressing any button.
  9. onMousePressed: This is MouseEvent handler type property. It is assigned by the user to particular function, when the node encounters action of mouse pressing.
  10. onMouseReleased: This is MouseEvent handler type property. It is assigned by the user to particular function, when the node encounters action of mouse releasing.
  11. onRotate: This is RotateEvent handler type property. It is assigned by the user to particular function, when the node encounters action of rotation on particular node.
  12. onRotationStarted: This is RotateEvent handler type property. It is assigned by the user to particular function, when the node encounters action of starting rotation on particular node.
  13. onRotationFinished: This is RotateEvent handler type property. It is assigned by the user to particular function, when the node encounters action of finishing rotation on particular node.
  14. onScroll: This is ScrollEvent handler type property. It is assigned by the user to particular function, when the node encounters action of scrolling on particular node.

The above properties used with setters helps to handle various events hence they are called as convenience methods, for e.g.

JavaFX Convenience Method

Example:

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 EventUI extends Application{  
@Override  
public void start(Stage primaryStage) throws Exception {  
  
    Circle c = new Circle(100,100,50);  
    c.setFill(Color.RED);  
    c.setStroke(Color.BLACK);  
      
    Button btn = new Button("Move");  
    btn.setTranslateX(145);  
    btn.setTranslateY(220);  
      
    Button btn1 = new Button("Stop");  
    btn1.setTranslateX(195);  
    btn1.setTranslateY(220);  
      
    TranslateTransition trans = new TranslateTransition();  
    trans.setAutoReverse(true);  
    trans.setByX(200);  
    trans.setCycleCount(100);  
    trans.setDuration(Duration.millis(500));  
    trans.setNode(c);  
      
    EventHandler<MouseEvent> handler = new EventHandler<MouseEvent>() {  
  
        @Override  
        public void handle(MouseEvent event) {  
            // TODO Auto-generated method stub  
  
            if(event.getSource()==btn)  
            {  
            trans.play();
            }  
            if(event.getSource()==btn1)  
            {  
                trans.pause();
            }  
            event.consume();  
        }  
          
    };  
      
    btn.setOnMouseClicked(handler);  
    btn1.setOnMouseClicked(handler);  
      
    Group root = new Group();  
    root.getChildren().addAll(c,btn,btn1);  
    Scene scene = new Scene(root,420,300);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle("Javafx Convinience methods example");  
    primaryStage.show();  
}  
public static void main(String[] args) {  
    launch(args);  
}  
} 

Output:

JavaFX Convenience Methods JavaFX Convenience Methods JavaFX Convenience Methods

Explanation:

In order to create the event on any given components in JavaFX using convenience method, we have to import all the required libraries. Then we have created one class named EventUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold various components with the event handlers, a Group object is created which is then passed to the Scene class object.

The circle is created using the constructor. Then the properties are set using the setters method. Two buttons are created one to move and one to stop the moving circle. The setOnMouseClicked() convenience method I used to register the event.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with title “ Javafx Convinience methods example”. Also, it displays circle which will move when move button clicked and stop when stop button pressed.