JavaFX Event Handling Examples PDF

Summary

This document provides examples of JavaFX event handling using different approaches. It details the use of lambda expressions, separate classes, and anonymous inner classes for creating event handlers. There is also detailed explanation and code examples included. This is a good resource for learning to handle events in JavaFX.

Full Transcript

# Java Fx ## 4 Main Library Imports - `javafx.application.Application` - `javafx.fxml.FXMLLoader` - `javafx.scene.Scene` - `javafx.stage.Stage` ## Provide Code with `@Override` The `HelloApplication` class extends `Application`. This class requires two methods: - `start()` - `main()` ### `Overri...

# Java Fx ## 4 Main Library Imports - `javafx.application.Application` - `javafx.fxml.FXMLLoader` - `javafx.scene.Scene` - `javafx.stage.Stage` ## Provide Code with `@Override` The `HelloApplication` class extends `Application`. This class requires two methods: - `start()` - `main()` ### `Override` - Public void start(Stage stage) throws IOException - **Step 1: Load FXML File** - `FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("Hello.fxml"));` - **Step 2: Create and Load Scene** - `Scene scene = new Scene(fxmlLoader.load(), 420, 520);` - **Step 3: Set Title for Stage and Set the Scene** - `stage.setTitle("Hello");` - `stage.setScene(scene);` - `stage.show();` ### Public Static Void Main(String[] Args) - launch(); ## Separate Class Event Handler ### Event Handler Class - `import javafx.event.*` - `public class Event Handler implements javafx.event.EventHandler<ActionEvent>` - `@Override` - `public void handle(ActionEvent actionEvent) {` - `System.out.println("Test");` - `}` ## Hello Controller - `public class HelloController {` - `import javafx.scene.control.*` - `@FXML` - `private Button button;` - `public void initialize() {` - `button.setOnAction(new EventHandler());` - `}` ## Without Controller (No FXML Loading) ### Button Event Handler - `public class ButtonEventHandler implements EventHandler <ActionEvent > {` - `public void handle(ActionEvent actionEvent) {` - `System.out.println("Hello World");` ## Main Class To Run Application - `import javafx.event.*; / javafy.Control.*; /javafy. Stene.layout` - `public class MainApplication extends Application {` - `@Override` - `public void start(Stage stage) throws IOException {` - `VBox root = new VBox();` - `Button helloButton = new Button("Text here");` - `helloButton.setOnAction(new ButtonEventHandler());` - `root.getChildren().add(helloButton);` - `Scene scene = new Scene(root);` - `stage.setTitle("Hello App");` - `stage.setScene(scene);` - `stage.show();` ### Public Static Void Main(String[] Args) - `launch();` ## Example 2: Mouse Event (Mouse Click or Movements) - `import javafx.scene.input.MouseEvent;` - `import javafx.event.*;` - `public class MouseEventHandler implements javafx.event.EventHandler<MouseEvent> {` - `@Override` - `public void handle(MouseEvent mouseEvent) {` - `System.out.println("Mouse Event: " + mouseEvent);` - `}` ## Main Method - `public static void main(MainApplication extends Application {` - `@Override` - `public void start(Stage stage) throws IOException {` - `VBox root = new VBox();` - `Label label = new Label("Hover here");` - `label.setOnMouseEntered(new MouseEventHandler());` - `root.getChildren().add(label);` - `Scene scene = new Scene(root, 500, 400);` - `stage.setTitle("Mouse Test");` - `stage.setScene(scene);` ## Inner Class Event Handling - `Inner class is basically creating a class in the main to handle event handling.` - `public class MainApplication extends Application {` - `@Override` - `public void start(Stage stage) throws IOException {` - `StackPane root = new StackPane();` - `Button button = new Button("Button here");` - `button.setOnAction(new InnerButtonEventHandler());` - `Label label = new Label("Label Here");` - `label.setOnMouseClicked(new InnerMouseEventHandler());` - `root.getChildren().addAll(button, label);` - `stage.setTitle("Hello Application");` - `stage.setScene(root);` - `stage.show();` ### Private Class Inner Buttom Event Handler - `private class InnerButtonEventHandler implements javafx.event.EventHandler<ActionEvent>{` - `public void handle(ActionEvent actionEvent) {` - `System.out.println("Hello World");` - `}` ### Private Class Inner Mouse Event Handler - `private class InnerMouseEventHandler implements EventHandler<MouseEvent>` - `System.out.println("Testing Mouse");` ### Public Static Void Main(String[] Args) - `launch(args);` ## Anonymous Inner Class - `public class AnonymousMainApplication extends Application {` - `public static void main(String[] args) {` - `launch(args);` ### Public Void Start (Stage Stage) Throws IOException - `StackPane root = new StackPane();` - `Button button = new Button("Button Here");` - `button.setOnAction(new EventHandler<ActionEvent>() {` - `public void handle(ActionEvent actionEvent) {` - `System.out.println("Hello");` - `}` - `root.getChildren().add(button);` - `Scene scene = new Scene(root, 630, 550);` - `stage.setTitle("Hello");` - `stage.setScene(scene);` - `stage.show();` ## Lambda Event Handler - `The easiest way to write the event handling code.` - `public class LambdaMainApplication extends Application {` - `public static void main(String[] args) {` - `launch(args);` ### Public Void Start (Stage Stage) - `StackPane root = new StackPane();` - `Button button = new Button("Hello");` - `button.setOnAction(event -> { System.out.println("Hello"); });` ### Text Field - `TextField textField = new TextField();` - `textField.setOnMouseEntered(event -> { System.out.println("Hello"); });`

Use Quizgecko on...
Browser
Browser