JavaFX
JavaFX is a set of graphics and media packages that enables developers to design, create, test, debug, and deploy rich client applications that operate consistently across diverse platforms.
Why JavaFX?
- Rich set of APIs: JavaFX provides a rich set of APIs for creating sophisticated GUI applications.
- Cross-platform: JavaFX applications can run on various platforms, including desktop, mobile, and embedded systems.
- Hardware acceleration: JavaFX uses hardware acceleration to provide a smooth and responsive user experience.
- CSS styling: You can use CSS to style your JavaFX applications, making it easy to create a custom look and feel.
Getting Started with JavaFX
To create a JavaFX application, you need to extend the javafx.application.Application class and implement the start() method. The start() method is the main entry point for all JavaFX applications.
Example: A Simple "Hello World" Application
Here's a simple "Hello World" application in JavaFX:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Label label = new Label("Hello World, JavaFX!");
StackPane root = new StackPane();
root.getChildren().add(label);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Explanation
Applicationclass: Every JavaFX application must extend theApplicationclass.start(Stage primaryStage)method: This is the main entry point for the application. TheStageis the top-level container (the window).Label: ALabelis a simple control for displaying text.StackPane: AStackPaneis a layout pane that places all its children in a single stack.Scene: AScenerepresents the content of aStage.primaryStage.show(): This method shows the window.main(String[] args)method: Themainmethod is not required for JavaFX applications in some environments, but it's good practice to include it for compatibility. Thelaunch(args)method is called to start the JavaFX application.
Running the Application
To run this application, you need to have the JavaFX SDK set up in your project. If you are using an IDE like IntelliJ or Eclipse, you can configure it to use the JavaFX library.
Configuring JavaFX in Eclipse
Since JavaFX is no longer bundled with the standard JDK, you need to configure your Eclipse project to use it.
Prerequisites:
- JDK 11 or later: Ensure you have a compatible JDK installed.
- JavaFX SDK: Download the JavaFX SDK from the GluonHQ website. Unzip it to a memorable location.
Steps:
Create a Java Project:
- In Eclipse, go to
File > New > Java Project. - Give it a name and select your JDK.
- In Eclipse, go to
Create a User Library for JavaFX:
- Right-click the project >
Build Path > Configure Build Path.... - Go to the
Librariestab and selectClasspath. - Click
Add Library...>User Library>Next. - Click
User Libraries...>New.... - Give it a name (e.g.,
JavaFX17) and clickOK. - Select your new library and click
Add External JARs.... - Navigate to the
libfolder of your downloaded JavaFX SDK and add all the.jarfiles. - Click
Apply and Close.
- Right-click the project >
Create your Application Class:
- Create a new class and add the
HelloWorldexample code.
- Create a new class and add the
Set VM Arguments for the Run Configuration:
- Go to
Run > Run Configurations.... - Select your application under
Java Application. - Go to the
Argumentstab. - In the
VM argumentsbox, add the following, replacing/path/to/your/javafx-sdk/libwith the actual path to your JavaFX SDK'slibfolder:--module-path /path/to/your/javafx-sdk/lib --add-modules javafx.controls,javafx.fxml - Click
Apply, thenRun.
- Go to
Troubleshooting
Error: Unsupported major.minor version 67.0 (or similar)
If you see an error like java.lang.module.FindException: Error reading module ... Caused by: java.lang.module.InvalidModuleDescriptorException: Unsupported major.minor version..., it means your JavaFX SDK version is not compatible with your project's JDK version.
Solution:
Check your Project's JDK Version:
- In Eclipse, right-click your project >
Build Path > Configure Build Path.... - Under the
Librariestab, find the JRE System Library. It will show your Java version (e.g.,[JavaSE-21]).
- In Eclipse, right-click your project >
Download the Matching JavaFX SDK:
- Go to the GluonHQ website and download the JavaFX SDK version that exactly matches your project's JDK version. For example, if your project uses JDK 21, you must download JavaFX SDK 21.
Update Your Project:
- Update your User Library to point to the new, version-matched JavaFX SDK
libfolder. - Update the
--module-pathin your Run Configuration's VM arguments to point to the correct SDK path.
- Update your User Library to point to the new, version-matched JavaFX SDK