JavaFX and NetBeans

This section explains how to create a JavaFX application in NetBeans. JavaFX 15.0.1 and Apache NetBeans 12.2 were used for the IDE screenshots.

Download an appropriate JDK for your operating system and set JAVA_HOME to the JDK directory. Refer to Install Java section for more information.

You can create a JavaFX 11 modular or non-modular project and use the IDE tools, Maven or Gradle build tools.

Note: We recommend you to use NetBeans 11.3 or later. See the NetBeans bug report NETBEANS-3296, "Attaching JavaFX Javadoc and Sources," for details on the errors that can occur when trying to use the JavaFX API documentation and source code in NetBeans, along with techniques to work around some of the errors.

Non-modular projects

IDE

Follow these steps to create a JavaFX non-modular project and use the IDE tools to build it and run it. Alternatively, you can download a similar project from here.

Download the appropriate JavaFX SDK for your operating system and unzip it to a desired location, for instance /Users/your-user/Downloads/javafx-sdk-11.

Create a new global Library under Tools -> Libraries -> New Library. Name it JavaFX11 and include the jars under the lib folder from JavaFX 11. Create library

Note: Make sure you don't add the src.zip file, as it will cause an exception when running the project.
1. Create a Java project

Create a Java project Provide a name to the project, like HelloFX, and a location. A default project will be opened.

Warning: Don't try to create a JavaFX project. The JavaFX Ant tasks of the current Apache NetBeans version are not ready for JavaFX 11+ yet, unless you have a custom JDK that bundles JavaFX, as described in Custom JDK+JavaFX image.
2. Set JDK

Make sure your project is configured to run with JDK 11 or later. Go to Properties -> Libraries -> Java Platform, and set it to your preferred JDK. Set JDK 11

3. Add the library

Go to Properties -> Libraries -> Compile -> Classpath -> + -> Add Library and add the JavaFX11 library. Add jars to classpath And go to Properties -> Build -> Compile and make sure you deselect the Compile on Save option. Remove option

Warning: If NetBeans compiles on every save, it will also add the classes to the module-path, preventing any further change to the module-path. Alternatively, this option can be kept selected, and add the JavaFX11 library to Properties -> Libraries -> Run -> Modulepath.

Once the classpath is set, the JavaFX classes will be recognized by the IDE. For instance, you can start extending the Application class: JavaFX jars as library

4. Add JavaFX classes

You can add a main class Main, based on this one, with an FXML file and a controller. Add JavaFX classes

Warning: If you run now the project it will compile but you will get this error:

Error: JavaFX runtime components are missing, and are required to run this application
This error is shown since the Java 11 launcher checks if the main class extends javafx.application.Application. If that is the case, it is required to have the javafx.graphics module on the module-path.
5. Add VM options

To solve the issue, go to Properties -> Run and add these VM options:


--module-path /path/to/javafx-sdk-11/lib --add-modules javafx.controls,javafx.fxml

--module-path "\path\to\javafx-sdk-11\lib" --add-modules javafx.controls,javafx.fxml

VM options Click apply and close the dialog.

6. Run the project

Click Run -> Run Project to run the project, now it should work fine.

Maven

Follow these steps to create a JavaFX non-modular project and use the Maven tools to build it and run it. Alternatively, you can download a similar project from here.

1. Create a Maven project

You can select either Java with Maven -> Simple JavaFX Maven Archetype project or Java with Maven -> FXML JavaFX Maven Archetype if your project is using FXML. Create a Maven project

Provide the name of the project, like hellofx and a location. Provide the groupId, like org.openjfx, and the package name, like org.openjfx.hellofx. Optionally, you can set the javafx-version to 12 and the javafx-maven-plugin-version to to 0.0.1: Archetype options Click finish and the project will be created and opened.

2. Verify the pom

You can find the generated pom file here.

Verify it includes the javafx.controls and javafx.fxml dependencies, update their version to 12, and verify it includes the javafx-maven-plugin (update the version to 0.0.1). Note that Maven manages the required dependencies: it will add javafx.base and javafx.graphics that are required by javafx.controls and javafx.fxml, but most important, it will add the required classifier based on your platform, downloading the specific platform jars. As for any other maven dependencies, these jars can be found in the local .m2 repository. Maven pom For a non-modular project, you can remove the module-info.java file.

3. Run the project

Click Run -> Run Project to run the project. Note that the Maven project already includes an nbactions.xml file that maps the run action to the javafx:run goal.

Note: In case JAVA_HOME is not set to 11 or greater, running the project might fail. To avoid it, you can add the correct java command to the javafx-maven-plugin: <configuration><executable>/path/to/jdk-11/bin/java</executable></configuration>.

You can also open a terminal and type mvn clean javafx:run to run the project.

Gradle

Follow these steps to create a JavaFX non-modular project and use the Gradle tools to build it and run it. Alternatively, you can download a similar project from here.

Since NetBeans 11, Gradle is supported out of the box, but if you run an older version, you will need the Gradle plugin for NetBeans 2.0.2. You can download it from the GitHub repository.

1. Create a Gradle project

Create a Java with Gradle -> Java Application project. Create a Gradle project Provide the name of the project, like HelloFX and a location for the project, provide the package name, like org.openjfx and the main class, like MainApp. Finally, select Initialize Gradle Wrapper. Name project The project is created and opened.

Note: Currently, the JavaFX Gradle plugin requires that the gradle wrapper is set to a top value of 6.3. Set the value of distributionUrl accordingly in the gradle/wrapper/gradle-wrapper.properties file.
2. Modify the build

Edit the build.gradle file and replace it with this build file.

We can declare the required JavaFX modules in the build.gradle file using the JavaFX gradle plugin:


javafx {
    version = "12"
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}
Note: If NetBeans is running with a JDK lower than 11 the plugin might not work. Either edit the netbeans/etc/netbeans.conf file and set netbeans_jdkhome accordingly, or do the same with the ~/.gradle/gradle.properties file, setting org.gradle.java.home.

Save the project and you will get the JavaFX dependencies. Update the build As for any other Gradle dependencies, these jars can be found in the local .gradle repository.

3. Add the source code

Based on this MainApp class, add its content to the project main class. Then add the controller and the FXML and and the css files. Add JavaFX project

4. Run the project

You can directly run from the Run Project button. But you can also select the build.gradle file and visualize the Navigator window. Build the project with Build -> build and click on Application -> run to execute the project. Run project

You can also open a terminal and run:


./gradlew run

gradlew run

Modular projects

Download the appropriate JavaFX jmods for your operating system and unzip it to a desired location, for instance /Users/your-user/Downloads/javafx-jmods-11.

IDE

Follow these steps to create a JavaFX modular project and use the IDE tools to build it and run it. Alternatively, you can download a similar project from here.

1. Create a Java modular project

Java modular project Provide a name to the project, like HelloFX, and a location. Select also platform JDK 11 or greater. When the project opens, right click on it and add New -> Module..., named it hellofx, and add a package like org.openjfx with an empty MainApp class.

2. Add the library

As explained for Non-modular projects, define the JavaFX11 library, if you haven't done it already.

Go to Properties -> Libraries -> Compile -> Modulepath -> + -> Add Library and add the JavaFX11 library. JavaFX Library

3. Edit the module-info class

Edit the module-info class and include the required modules javafx.controls and javafx.fxml. Since FXML uses reflection to access the controller in the module, this has to be opened to javafx.fxml. Finally, export the package org.openjfx. module-info

4. Add the source code

Based on this MainApp class, add its content to the project main class. Then add the controller and the FXML and and the css files. Source code

5. Run the project

Being a modular project, and since we already added the JavaFX11 library to the module-path, there is no need to add any VM arguments. Click Run -> Run... to run the project.

6. Create a custom runtime image

To create a runtime image, create a global Library under NetBeans -> Tools -> Libraries -> New Library. Name it JavaFXMODS11 and include the folder with the JavaFX jmods 11. Add this library to NetBeans -> Properties -> Libraries -> Run -> Modulepath and place it on top. jmods library

To create the custom runtime image now, go to NetBeans -> Properties -> Build -> Packaging and select Create JLINK distribution, providing a name for the launcher, like HelloFX. jlink task Apply and close the dialog, and click the Clean and Build button, to build the image.

To run the image:


dist/jlink/HelloFX/bin/HelloFX

dist\jlink\HelloFX\bin\HelloFX

Maven

Follow these steps to create a JavaFX modular project and use the Maven tools to build it and run it. Alternatively, you can download a similar project from here.

1. Create a Maven project

You can select either Java with Maven -> Simple JavaFX Maven Archetype project or Java with Maven -> FXML JavaFX Maven Archetype if your project is using FXML. Create a Maven project

Provide the name of the project, like hellofx and a location. Provide the groupId, like org.openjfx, and the package name, like org.openjfx.hellofx. Optionally, you can set the javafx-version to 12 and the javafx-maven-plugin-version to to 0.0.1: Archetype options Click finish and the project will be created and opened.

2. Verify the pom

You can find the generated pom file here.

Verify it includes the javafx.controls and javafx.fxml dependencies, and verify it includes the javafx-maven-plugin. Note that Maven manages the required dependencies: it will add javafx.base and javafx.graphics that are required by javafx.controls and javafx.fxml, but most important, it will add the required classifier based on your platform, downloading the specific platform jars. As for any other maven dependencies, these jars can be found in the local .m2 repository. Maven pom

3. Run the project

Click Run -> Run Project to run the project. Note that the Maven project already includes an nbactions.xml file that maps the run action to the javafx:run goal.

Note: In case JAVA_HOME is not set to JDK 11 or greater, running the project might fail. To avoid it, you can add the correct java command to the javafx-maven-plugin: <configuration><executable>/path/to/jdk-11/bin/java</executable></configuration>.

You can also open a terminal and run mvn clean javafx:run to run the project.

6. Create a custom runtime image

To create a runtime image, run the following commands:


mvn clean javafx:jlink

Note the plugin allows the usual options as the jlink command, as well as creating a launcher or a zip with the custom image.

And after the image is built, you can run it from command line:


target/hellofx/bin/launcher

target\hellofx\bin\launcher

Gradle

Follow these steps to create a JavaFX modular project and use the Gradle tools to build it and run it. Alternatively, you can download a similar project from here.

1. Create a Gradle project

Create a Java with Gradle -> Java Application project. Create a Gradle project Provide the name of the project, like HelloFX and a location for the project, provide the package name, like org.openjfx and the main class, like MainApp. Finally, select Initialize Gradle Wrapper. The project is created and opened.

Note: Currently, the JavaFX Gradle plugin requires that the gradle wrapper is set to a top value of 6.3. Set the value of distributionUrl accordingly in the gradle/wrapper/gradle-wrapper.properties file.
2. Modify the build

Edit the build.gradle file and replace it with this build file.

We can declare the required JavaFX modules in the build.gradle file using the JavaFX gradle plugin:


javafx {
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}
Note: If NetBeans is running with a JDK lower than 11 the plugin might not work. Either edit the netbeans/etc/netbeans.conf file and set netbeans_jdkhome accordingly, or do the same with the ~/.gradle/gradle.properties file, setting org.gradle.java.home.

Save the project and you will get the JavaFX dependencies. As for any other Gradle dependencies, these jars can be found in the local .gradle repository.

3. Add the module-info class

Add the module-info class, including the required modules javafx.controls and javafx.fxml. Since FXML uses reflection to access the controller in the module, this has to be opened to javafx.fxml. Finally, export the package org.openjfx. module-info Reload the project to take into account all the changes in the gradle project.

4. Add the source code

Based on this MainApp class, add its content to the project main class. Then add the controller and the FXML and the css files. Source files

5. Run the project

Right click on the project window, select Tasks -> build -> build to build the project, and on Tasks -> run -> run to execute the project. You can also open a terminal and run:


./gradlew run

gradlew run
6. Create a custom runtime image

To create a custom runtime, you can use the org.beryx.jlink plugin. It can be easily combined with the JavaFX Gradle plugin:


plugins {
  id 'application'
  id 'org.openjfx.javafxplugin' version '0.0.7'
  id 'org.beryx.jlink' version '2.9.4'
}

javafx {
    version = "12"
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}

jlink {
    launcher {
        name = 'hellofx'
    }
}

to generate the custom image. Right click on the project window, select Tasks -> jlink -> jlink task to create the image.

To run the image, run from a terminal:


build/image/bin/hellofx

build\image\bin\hellofx