JavaFX

JavaFX

Download JavaFX (opens in a new tab) and extract it locally to {JAVAFX_PATH}: /Users/user/javafx-sdk-24, for example.

Code

  1. Import JavaFX’s components (all the *.jar files under lib) into your project as referenced libraries. This will update .vscode/settings.json:

    .vscode/settings.json
    {
      "java.project.sourcePaths": ["src"],
      "java.project.outputPath": "bin",
      "java.project.referencedLibraries": [
        "lib/**/*.jar",
        "{JAVAFX_PATH}/lib/**/*.jar"
      ]
    }
  2. Create .vscode/launch.json using the Run and Debug view, then update your configuration with the following settings:

    .vscode/launch.json
    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "java",
          "name": "Application",
          "request": "launch",
          "mainClass": "App",
          "vmArgs": "--module-path {JAVAFX_PATH}/lib --add-modules javafx.controls"
        }
      ]
    }
  3. Launch the newly created configuration by selecting it from the dropdown menu in the Run and Debug view then running it using Start Debugging (F5).

IDEA

Follow the Create a new JavaFX project (opens in a new tab) guide from JetBrains.

Eclipse

Follow the Getting Started with JavaFX (opens in a new tab) guide from OpenJFX:

  1. Open the marketplace using HelpEclipse Marketplace then search for and install the e(fx)clipse (opens in a new tab) plugin. Agree to the license terms and wait—this will take a while—until you receive a message asking you to restart Eclipse to apply the update.
  2. Select SettingsJavaFX then specify the location of the JavaFX directory ({JAVAFX_PATH}) in JavaFX 11+ SDK.
  3. Select SettingsJavaBuild PathUser Libraries then click on New to create a new user library named JavaFX. Click on Add External JARs and select all the *.jar files under {JAVAFX_PATH}/lib and add them.
  4. Select ProjectPropertiesJava Build PathLibrariesModulepath then Add LibraryUser Library and select JavaFX.
  5. Select SettingsRun/DebugString Substitution and create a new variable FX_PATH with the value {JAVAFX_PATH}/lib.
  6. Select RunRun ConfigurationsJava ApplicationArguments, set VM Arguments to --module-path ${FX_PATH} --add-modules javafx.controls and uncheck Use the -XstartOnFirstThread argument when launching with SWT.

Sample

Main.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
 
public class Main extends Application {
  @Override
  public void start(Stage primaryStage) {
    try {
      BorderPane root = new BorderPane();
      Scene scene = new Scene(root, 400, 400);
      primaryStage.setScene(scene);
      primaryStage.show();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  public static void main(String[] args) { launch(args); }
}