Hello JavaFX

Let us create a simple JavaFX application. compile it and run it.

  1. Create a HelloFX.java file the following code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HelloFX extends Application {
    @Override
    public void start(Stage stage) {
        Label l = new Label("Hello JavaFX!");
        Scene scene = new Scene(new StackPane(l), 640, 480);
        stage.setScene(scene);
        stage.show();
    }
}

This file contains a basic JavaFX application which shows a Label.

  1. Compile the Java file:
javac --module-path $PATH_TO_FX --add-modules javafx.controls HelloFX.java
javac --module-path %PATH_TO_FX% --add-modules javafx.controls HelloFX.java

Add any additional modules used by the application. For example, if it is using FXML functionality, add the javafx.fxml module:

javac --module-path $PATH_TO_FX --add-modules javafx.controls,javafx.fxml HelloFX.java
javac --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml HelloFX.java

Transitive modules are automatically resolved. For instance, there is no need to add javafx.graphics module, since it is transitively required by the javafx.controls module.

More information about modules and module-path is available at Java Platform Module System specification.

  1. Run the application:
java --module-path $PATH_TO_FX --add-modules javafx.controls HelloFX
java --module-path %PATH_TO_FX% --add-modules javafx.controls HelloFX

Checkout the OpenJFX samples repository for more samples.