MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Warm up
  • 1: Collections
  • 2: More OOP
  • 3: Exceptions, Threads & File
  • 3: Other Concepts
  • 4: Graphical User Interface (GUI)
    • JavaFX
    • Event-Driven Programming and Animations

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

  • Application class: Every JavaFX application must extend the Application class.
  • start(Stage primaryStage) method: This is the main entry point for the application. The Stage is the top-level container (the window).
  • Label: A Label is a simple control for displaying text.
  • StackPane: A StackPane is a layout pane that places all its children in a single stack.
  • Scene: A Scene represents the content of a Stage.
  • primaryStage.show(): This method shows the window.
  • main(String[] args) method: The main method is not required for JavaFX applications in some environments, but it's good practice to include it for compatibility. The launch(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:

  1. Create a Java Project:

    • In Eclipse, go to File > New > Java Project.
    • Give it a name and select your JDK.
  2. Create a User Library for JavaFX:

    • Right-click the project > Build Path > Configure Build Path....
    • Go to the Libraries tab and select Classpath.
    • Click Add Library... > User Library > Next.
    • Click User Libraries... > New....
    • Give it a name (e.g., JavaFX17) and click OK.
    • Select your new library and click Add External JARs....
    • Navigate to the lib folder of your downloaded JavaFX SDK and add all the .jar files.
    • Click Apply and Close.
  3. Create your Application Class:

    • Create a new class and add the HelloWorld example code.
  4. Set VM Arguments for the Run Configuration:

    • Go to Run > Run Configurations....
    • Select your application under Java Application.
    • Go to the Arguments tab.
    • In the VM arguments box, add the following, replacing /path/to/your/javafx-sdk/lib with the actual path to your JavaFX SDK's lib folder:
      --module-path /path/to/your/javafx-sdk/lib --add-modules javafx.controls,javafx.fxml
      
    • Click Apply, then Run.

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:

  1. Check your Project's JDK Version:

    • In Eclipse, right-click your project > Build Path > Configure Build Path....
    • Under the Libraries tab, find the JRE System Library. It will show your Java version (e.g., [JavaSE-21]).
  2. 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.
  3. Update Your Project:

    • Update your User Library to point to the new, version-matched JavaFX SDK lib folder.
    • Update the --module-path in your Run Configuration's VM arguments to point to the correct SDK path.
Privacy Policy | Terms & Conditions