×

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 Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

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 Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

3 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX Arc

In the JavaFX application, in order to draw an arc, the Arc class is used. The Arc allows us to join any points in the space. All methods needed for...

5 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 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 Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Light Distant

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

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 Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 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 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 TilePane

In the JavaFX application, in order to set TilePane as a layout, the TilePane class is used. The TilePane layout allows us to arrange the components in the same sized...

4 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX Path Transition

In the JavaFX application, in order to generate the path transition, we have to apply path transition on any particular shape. It has the ability to traverse the given shape...

3 minutes read.