Object Oriented Programming: Unit V: JAVAFX Event Handling, Controls and Components

JAVAFX Handling Key and Mouse Events

with Example Java Programs

Mouse event is fired when the mouse button is pressed, released, clicked, moved or dragged on the node.

Handling Key and Mouse Events

Handling Mouse Event

Mouse event is fired when the mouse button is pressed, released, clicked, moved or dragged on the node.

Following table shows the user actions and associated event handling activities -

Various mehods for MouseEvent class are enlisted in the following table


There are Four constants-PRIMARY, SECONDARY, MIDDLE, and NONE - are defined in MouseButton to indicate the left, right, middle, and none mouse buttons.

Programming Example

package myjavafxapplication;

import javafx.application. Application;

import javafx.event.EventHandler;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.input. MouseEvent;

import javafx.scene.paint.Color;

import javafx.scene.shape: Rectangle;

import javafx.scene.text.Text;

import javafx.stage.Stage;

public class MyJavaFXApplication extends Application {

@Override

public void start(Stage primaryStage) {

Rectangle rect = new Rectangle(50,50,100,100);

rect.setArcWidth(20);

rect.setArcHeight(20);

rect.setFill(Color.BLUE);

Text text = new Text (20,20,"Click on the rectangle to change its color");

//Handling the mouse event

rect.setOnMouseClicked(e->{

rect.setFill(Color.RED);

});

Group root = new Group (rect,text);

Scene scene = new Scene(root,300,200);

primaryStage.setScene(scene);

primaryStage.setTitle("Mouse Event Demo");

primaryStage.show();

}

public static void main(String[] args) {

launch(args);

}

}

Output

Program Explanation: In above program,

(1) We have created one rectangular object and it is colored using blue color.

(2) The mouse click event is used. When user clicks over the rectangle, the color of the rectangle gets changed to red.

(3) Mouse Event is created and handled using following code

rect.setOnMouseClicked(e->{

rect.setFill(Color.RED);

});

Example 5.3.1: Write a JavaFX program to display the mouse position in the form of (x,y) co-ordinates when the text present on the screen is dragged somewhere else.

Sol. :

package myjavafxapplication;

import javafx.application.Application;

import javafx.event.EventHandler;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.control.Label;

import javafx.scene.input.MouseEvent;

import javafx.scene.text.Text;

import javafx.stage.Stage;

public class MyJavaFXApplication extends Application {

@Override

public void start(Stage primaryStage) {

Text text = new Text (20,20,"Move me anywhere on the screen");

Label label = new Label();

//Handling the mouse event

text.setOnMouseDragged(e->{

text.setX(e.getX());

text.setY(e.getY());

String msg="x:"+e.getX()+",y:"+e.getY();

label.setText("Text Coordinates: "+msg);

Group root = new Group(text,label);

Scene scene = new Scene(root,300,200);

primaryStage.setScene(scene);

primaryStage.setTitle("Mouse Event Demo");

primaryStage.show();

}

public static void main(String[] args) {

launch(args);

}

}

Output

Keyboard Events

When any key on the keyboard is pressed, released, or typed on a node then the keyboard event occurs.

The associated keyboard event triggering actions made by user, type of event and event registration methods are enlisted in the following table.

For recognizing the keyboard events we use KeyEvent class. Various methods of KeyEvent class are as described in the following table.

The key codes returned by the getCode() method are constants. These constants are enlisted in the following table -

Following is a simple keyboard event handling program that displays the character typed on the output window.

Programming Example

package myjavafxapplication;

import javafx.application. Application;

import javafx.event.EventHandler;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.input.MouseEvent;

import javafx.scene.text.Font;

import javafx.scene.text.FontWeight;

import javafx.scene.text.Text;

import javafx.stage.Stage;

public class MyJavaFXApplication extends Application {

@Override

public void start(Stage primaryStage) {

Text msg = new Text(20,20,"Type something here...");

Text text = new Text(50,50," ");

text.setFont(Font.font("Arial", FontWeight.BOLD,30));

//Creating the keyboard event handler

text.setOnKeyPressed(e->{

text.setText(e.getText());

});

Group root = new Group(msg,text);

Scene scene = new Scene(root, 300,200);

primaryStage.setScene(scene);

primaryStage.setTitle("KeyBord Event Demo");

primaryStage.show();

text.requestFocus();

}

public static void main(String[] args) {

launch(args);

}

}

Output

Program Explanation: In above code,

(1) For displaying the character typed we have set the Text node. As we want bold,arial and a

font size of 30 px, we use setFont method.

(2) Then the keybpard event is set using the lambda function

text.setOnKeyPressed(e->{

text.setText(e.getText());

});

Due to this event, the key being pressed is displayed.

Object Oriented Programming: Unit V: JAVAFX Event Handling, Controls and Components : Tag: : with Example Java Programs - JAVAFX Handling Key and Mouse Events