Starting a JavaFX Project with Gluon Tools

Avatar
Frank Delporte

Frank Delporte (@frankdelporte) is a Java developer, blogger, author of "Getting started with Java on Raspberry Pi", and contributor to Pi4J. Frank blogs about his experiments with Java, sometimes combined with electronic components, on the Raspberry Pi.

Here on foojay.io you can already find two posts by Carl Dea to get you started with JavaFX:

  1. 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.
  2. 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.

  1. Azul ZuluFX
  2. BellSoft LibericaJDK

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:

  1. Download the FX-version of your choice from the Gluon website
  2. Unpack it into a directory of your choice, e.g. opt/javafx-sdk-16/
  3. 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.
  4. 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.
Adding openjfx as a dependency
Defining the VM options to run your application

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.

Minimal selections for a JavaFX application on start.gluon.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.

Preview of the project before downloading

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.

The created application running in IntelliJ IDEA

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.

Topics:

Don’t Forget to Share This Post!

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Related Articles

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard