JavaFX Tutorial
JavaFX is a Java framework used for building rich internet applications. It provides a set of graphical user interface (GUI) components and a rich set of API for creating interactive applications. JavaFX allows developers to build applications that can run on various platforms including desktop, web, and mobile devices.
Getting Started with JavaFX
To get started with JavaFX, you need to have Java Development Kit (JDK) 8 or later installed on your system. JavaFX is included with the JDK, so you don't need to download or install anything separately. Once you have the JDK installed, you can start building JavaFX applications.
Creating a JavaFX Application
To create a JavaFX application, you need to create a class that extends the javafx.application.Application
class. This class provides a standard template for creating JavaFX applications. Let's create a simple JavaFX application that displays a window with a "Hello, World!" label.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorldApp extends Application {
@Override
public void start(Stage primaryStage) {
Label label = new Label("Hello, World!");
StackPane root = new StackPane(label);
Scene scene = new Scene(root, 300, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Hello World Application");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
In the above code, we create a Label
with the text "Hello, World!" and add it to a StackPane
. We then create a Scene
with the StackPane
as the root node and set the size of the scene to 300x200 pixels. Finally, we set the scene to the primary stage, set the title of the application, and show the stage.
To run the JavaFX application, you can simply run the main
method of the application class. This will launch the JavaFX runtime, which will create the application window and execute the start
method.
JavaFX Controls
JavaFX provides a rich set of controls that you can use to build user interfaces for your applications. Some of the commonly used controls include Button
, TextField
, ComboBox
, and TableView
. Let's see an example of using these controls in a JavaFX application.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ControlsApp extends Application {
@Override
public void start(Stage primaryStage) {
Label nameLabel = new Label("Name:");
TextField nameTextField = new TextField();
Button submitButton = new Button("Submit");
ComboBox<String> genderComboBox = new ComboBox<>();
genderComboBox.getItems().addAll("Male", "Female", "Other");
VBox root = new VBox(10);
root.getChildren().addAll(nameLabel, nameTextField, submitButton, genderComboBox);
Scene scene = new Scene(root, 300, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Controls Application");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
In the above code, we create a Label
for the name, a TextField
for entering the name, a Button
for submitting the form, and a ComboBox
for selecting the gender. We add these controls to a VBox
layout container, which arranges the controls vertically. We then create a Scene
with the VBox
as the root node and set the size of the scene to 300x200 pixels. Finally, we set the scene to the primary stage, set the title of the application, and show the stage.
JavaFX Events and Event Handling
JavaFX provides a powerful event handling mechanism that allows you to handle user interactions and respond to various events such as button clicks, mouse movements, and key presses. Let's see an example of handling a button click event in a JavaFX application.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class EventHandlingApp extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Click Me");
button.setOnAction(event -> System.out.println("Button clicked!"));
StackPane root = new StackPane(button);
Scene scene = new Scene(root, 300, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Event Handling Application");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
In the above code, we create a Button
with the text "Click Me" and attach an event handler to it using the setOnAction
method. The event handler is a lambda expression that prints a message to the console when the button is clicked.
Conclusion
This tutorial provided a brief introduction to JavaFX and demonstrated how to create a simple JavaFX application, use JavaFX controls, and handle events in