Here on foojay.io you can already find two posts by Carl Dea to get you started with JavaFX:
- Beginning JavaFX Applications with IntelliJ IDE: step-by-step how to start a new JavaFX project without any tools, are by using the Maven and Gradle tools.
- A JavaFX App on ZuluFX in 60 Seconds: how to use a popular distribution from Azul to build a JavaFX HelloWorld Application in 60 seconds.
In this post, I want to show you yet another approach that uses the tools provided by Gluon, who are the maintainers, and the driving force behind OpenJFX.
Before We Start...
We will be using IntelliJ IDEA in this post, and to develop JavaFX applications, we need to have the proper JDK and JavaFX installed.
In the past JavaFX was included in the Oracle JDK up till version 11.
But it has always been a separate project (see openjfx.io) and can also be installed separately from the JDK.
This gives us two possible approaches.
Use a JDK Including JavaFX
Some JDK providers have a version of the JDK which has JavaFX integrated. This makes it easy to build and run applications without the need to specify the module path and modules required to run your application.
The easiest way to switch between JDK versions is probably SDKMAN which contains a long list of possible options. For example on a Linux PC this is only a few of them of a list of over 80 versions!
$ sdk list java ================================================================================ Available Java Versions ================================================================================ Vendor | Use | Version | Dist | Status | Identifier -------------------------------------------------------------------------------- AdoptOpenJDK | | 15.0.1.j9 | adpt | | 15.0.1.j9-adpt ... | >>> | 11.0.7.hs | adpt | local only | 11.0.7.hs-adpt ... Azul Zulu | | 15.0.1 | zulu | | 15.0.1-zulu | | 15.0.1.fx | zulu | | 15.0.1.fx-zulu ... | | 8.0.272 | zulu | | 8.0.272-zulu | | 8.0.272.fx | zulu | | 8.0.272.fx-zulu | | 7.0.282 | zulu | | 7.0.282-zulu | | 6.0.119 | zulu | | 6.0.119-zulu BellSoft | | 15.0.1.fx | librca | | 15.0.1.fx-librca | | 15.0.1 | librca | | 15.0.1-librca | | 14.0.2.fx | librca | | 14.0.2.fx-librca ... | | 8.0.275.fx | librca | | 8.0.275.fx-librca | | 8.0.275 | librca | | 8.0.275-librca GraalVM | | 20.2.0.r11 | grl | | 20.2.0.r11-grl | | 20.2.0.r8 | grl | | 20.2.0.r8-grl ... ================================================================================ Use the Identifier for installation: $ sdk install java 11.0.3.hs-adpt ================================================================================
For JavaFX-development, select a version with ".fx", e.g. sdk install java 15.0.1.fx-zulu
and use this version in your IDE.
Use JavaFX as a Separate Library
This option allows you more flexibility to use separate versions of Java and JavaFX. In my case - as most of my projects or on Java 11 - I use AdoptOpenJDK 11, but combine it with JavaFX 16-ea to be able to test with the latest version.
In that case you'll need to follow these steps:
- Download the FX-version of your choice from the Gluon website
- Unpack it into a directory of your choice, e.g.
opt/javafx-sdk-16/
- In the "Project structure" dialog in IntelliJ IDEA add the location including the "lib" in the "Modules > Dependencies" screen, e.g.
/opt/javafx-sdk-16/lib
. By doing this, your IDE will be able to provide you the correct syntax highlighting. - To run your application, we also need to add the startup arguments to point to the javafx-modules. Open the Configurations dialog and provide the "VM options", including all the modules required for your project, e.g.
--module-path /opt/javafx-sdk-16/lib --add-modules javafx.controls,javafx.fxml
.
Create a New Project with start.gluon.io
Gluon has created the website start.gluon.io to start a new JavaFX project, the same way you can also do for Spring with start.spring.io, or Quarkus with code.quarkus.io.
For a minimal JavaFX project, you only need to define the "Application Details" and select the version and modules you are going to need.
As Gluon also offers additional features and you can use their tools to build mobile applications, extra options can be selected in the "Gluon Features" section. Be aware some of these require a license or a popup will be shown when your application starts.
A nice feature of this site is the "Preview Project" button which will show you the structure of your project before you download it.
By clicking the "Generate Project" button, you will get a ZIP with the full project you can open in your IDE and run immediately with mvn javafx:run
.
Create a New Mobile Project with the Gluon Plugin in IntelliJ IDEA
Gluon also created a plugin for IntelliJ IDEA which makes it very easy to start a JavaFX mobile project.
In your IntelliJ IDEA, double click the SHIFT key to open the universal search box and type "Plugins" and select the first option. Now you can search in the marketplace and install the Gluon plugin.
Starting a new mobile JavaFX project now has become a very easy process. From the main menu select File > New > Project. In the left list, you will now find "Gluon" and four different options for a new project.
In the next windows, you can select different options, Maven or Gradle, names of the pre-created first and second view,... Once the project is created and opened in IntelliJ IDEA, you can run it with mvn javafx:run
.
As you can see in this video, within 30 seconds you have a new JavaFX project opened in IntelliJ IDEA which you can immediately run and test.
Conclusion
The Gluon start website and the plugin allow you to get started with a new JavaFX project in a few clicks.
Thanks to the amazing work done by the Gluon team this also gives you a quick-start for the creation of a mobile application which can be built for both Android and iOS.
In a next post, we will see how the build process for these can be done with GitHub Actions.