×

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.


Related Topics

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 minutes read.

JavaFX Cylinder

In the JavaFX application, in order to draw a Cylinder, the Cylinder class is used. The Cylinder allows us to form a 3D shape having rectangular faces and a straight...

4 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX Fade Transition

In the JavaFX application, in order to generate the fade transition, we have to apply fade transition on any particular shape. It has the ability to show the fading colors...

4 minutes read.

JavaFX Slider

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range...

4 minutes read.

JavaFX Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX Sphere

In the JavaFX application, in order to draw a Sphere, the Sphere class is used. The Sphere allows us to form a 3D shape having circular faces. All methods needed...

4 minutes read.

JavaFX Light Spot

In the JavaFX application, in order to apply various Light Spot effect from a single source, we use this class. It has the ability to apply light from all the...

4 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX Blend

In the JavaFX application, in order to apply various blend effect, we use Blend class. It has the ability to blend the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis....

3 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life...

4 minutes read.

JavaFX CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

4 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX Area Chart

In the JavaFX application, in order to create Area Chart, the AreaChart class is used. The Area Chart allows us to plot data along the XY plane. All methods needed...

3 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.