×

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 are created. Event occurs when user interacts with an application. User can generate event by pressing keyboard button or by pressing mouse key or scrolling.

Events are classified into mainly two types:

1. Foreground events:

These are the events which occurs due to direct interaction of user with the application. Foregrounds events are clicking the button, pressing the mouse key and scrolling the page.

2. Background events:

These event doesn’t need the interaction from the user in the application. Background events are operating system interrupts, operation completion, and failures, etc.

In order to handle various events in javafx, we have to follow below steps:

1. Route Construction:

The default route of event is determined by creating the event dispatch chain. The node on which the event is generated is determined from stage using the event dispatch chain.

2. Event Capturing:

The nodes are traversed from top to bottom after the event dispatch chain is created to find the node of the event. If it founds event filter registered it will execute it. If event filter is not registered, the event is directly transferred to target. In this case event is procesed by the target node.

3. Event Bubbling:

When we reach to the target node again the event is traversed from bottom to top to find if any node is registered with event filter. If it is then particular event is executed else process is completed.

4. Event Handlers and Filters:

The main application logic for execution on any event is contained in event handlers and filters. All the event handlers and filters must implement javafx.event.EventHandler. One node can be assigned more than one event filters.

Properties associated with Event:

  1. Event Type: This is the instance of EventType class. It describes the type of event to be generated various events are registered with the event type class for example, we have KEY_PRESSED, KEY_TYPED, KEY_RELEASED to handle the key event.
  2. Source: This property is used to describe the event source. It is generally used to show origin which generates the event.
  3. Target: This describes the node on which the event is defined. It is the instance of the class which implements EventTarget interface.

JavaFX Event handling: Mouse Event

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;    
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;    
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent; 


public class EventUI extends Application {   
  
@Override  
public void start(Stage stage) throws Exception {    

Circle newcircle = new Circle(); 
     
newcircle.setCenterX(300.0f); 
newcircle.setCenterY(135.0f); 
     
newcircle.setRadius(75.0f); 
     
newcircle.setFill(Color.RED); 
     
newcircle.setStrokeWidth(20);      
      
     Text text = new Text("Please click on the circle to change it's color"); 
     
     text.setFont(Font.font(null, FontWeight.BOLD, 20));     
     
     text.setFill(Color.BLUE); 
 
     text.setX(150); 
     text.setY(50); 
      
     EventHandler<MouseEvent> eventHandler = new EventHandler<MouseEvent>() { 
        @Override 
        public void handle(MouseEvent e) { 
           newcircle.setFill(Color.DARKSLATEBLUE);
        } 
     };  
     newcircle.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);   
      
     Group root = new Group(newcircle, text); 
        
     Scene scene = new Scene(root, 600, 400); 
      
     scene.setFill(Color.LAVENDER);  
     
     stage.setTitle("Event Handling Example");       
        
     stage.setScene(scene); 
        
     stage.show(); 
  } 
  public static void main(String args[]){ 
     launch(args); 
  } 
}

Output:

JavaFX Event Handling

Explanation:

In order to create the mouse press event on given components in JavaFX using event handling, 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 handling, a Group object is created which is then passed to the Scene class object.

The label is created using the constructor and various properties are set using setters. Also, button is created using the constructor and various properties are set. The CSS effect on the label is applied by using the setStyle() method in java file.

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 “ Employee Information ”. Also, it displays the label for First name, Last name, Country name, and Language name with submit button.


Related Topics

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

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 Bloom

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

4 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

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 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.

JavaFX Color Input

In the JavaFX application, in order to apply various color input effect, we use the ColorInput class. It has the ability to fill the specified shape with a given color...

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 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 Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Rotation

In the JavaFX application, in order to apply various rotation effects, we use this class. It has the ability to rotate the given object. It rotates the given node along...

3 minutes read.

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 Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 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 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 Installation

In order to run JavaFX applications, we need to have eclipse and java installed on our machine. Prerequisite: Operating systemAny Operating system (Windows, Linux, Mac)Memory requirementNo minimum requirementDisk SpaceNo minimum requirementJDK versionJDK...

3 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 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 Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX Motion Blur

In the JavaFX application, in order to apply various Motion blur effect, we use Motion Blur class. It has the ability to apply the Motion blur effect on the given...

4 minutes read.