Clicking on a button usually triggers an event. For example, clicking the print button on the print dialog we meant to print a document to printer. UI library uses the events to notify something happended.
For each event, there would be information about the source and target controls.
JavaFX event is an instance of the javafx.event.Event class or its subclass.
JavaFX provides several events, including
You can define your own event by extending the Event
class.
The following table lists properties from Event
Property | Description |
---|---|
type | Type of event that occurred. For example, mouse event. |
Source | Origin of the event. |
Target | Last Node in the event dispatch chain. |
An event type is an instance of the EventType class. For example, the KeyEvent class contains the following event types:
The JavaFX event delivery process contains the following steps:
We can use the setOnXXX methods to register event handlers as the following format:
setOnEvent-type(EventHandler<? super event-class> value)
for example,
aButton.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { System.out.println("Hello World"); } });
Event-type
is the type of event that the handler processes,
for example, setOnKeyTyped for Key Typed events or
setOnMouseClicked for Mouse Clicked events.
event-class
is the class that defines the event type,
for example, KeyEvent for events related to keyboard input or
MouseEvent for events related to mouse input.
The following code shows how to handle key typed event when a key is pressed and released:
setOnKeyTyped(EventHandler<? super KeyEvent> value)
The following code shows how to handle button click event. The setOnAction() method is used to register an event handler. The handle() method in the event handler is called when user clicks the button and it print "Hello World" to the console.
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; /*from w ww . j a va2s . c o m*/ public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 300, 250); Button btn = new Button(); btn.setText("Hello World"); btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { System.out.println("Hello World"); } }); root.getChildren().add(btn); primaryStage.setScene(scene); primaryStage.show(); } }
The code above generates the following result.
setOnKeyPressed and setOnKeyReleased can register key event handlers.
import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.input.KeyEvent; import javafx.stage.Stage; /*www . j a v a2s. c o m*/ public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 300, 250); TextField textBox = new TextField(); textBox.setPromptText("Write here"); textBox.setOnKeyPressed(new EventHandler<KeyEvent>() { public void handle(KeyEvent ke) { System.out.println("Key Pressed: " + ke.getText()); } }); textBox.setOnKeyReleased(new EventHandler<KeyEvent>() { public void handle(KeyEvent ke) { System.out.println("Key Released: " + ke.getText()); } }); root.getChildren().add(textBox); primaryStage.setScene(scene); primaryStage.show(); } }
The code above generates the following result.
We can use setOnMouseEntered, setOnMouseExited, and setOnMousePressed to register mouse event handlers.
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.stage.Stage; /*from w w w .jav a 2 s. co m*/ public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 300, 250); Button btn = new Button(); btn.setText("Hello World"); btn.setOnMouseEntered(new EventHandler<MouseEvent>() { public void handle(MouseEvent me) { System.out.println("Mouse entered"); } }); btn.setOnMouseExited(new EventHandler<MouseEvent>() { public void handle(MouseEvent me) { System.out.println("Mouse exited"); } }); btn.setOnMousePressed(new EventHandler<MouseEvent>() { public void handle(MouseEvent me) { System.out.println("Mouse pressed"); } }); root.getChildren().add(btn); primaryStage.setScene(scene); primaryStage.show(); } }
The code above generates the following result.