User interface with JavaFX

JavaFX is a framework to create user interfaces for desktop (Windows, Mac, Linux) and mobile phones. JavaFX is an opensource project which is documented on openjfx.io and the sources are available in this GitHub project.

The main goal of Java has always been to be able to create applications which are “write once, run everywhere”. JavaFX promises the same for Graphical User Interface applications.

Gluon is the main maintainer of the OpenJFX project and offers commercial support to companies who want to use JavaFX in critical applications. They also provide tools to build and compile Java code to native applications for all platforms.

JavaFX on Raspberry Pi

JavaFX is also an ideal framework to build Java applications with a user interface for the Raspberry Pi!

You can find a runtime version dedicated to the Raspberry Pi on the Gluon download page. Let’s install it on our board, so we can start Java+JavaFX applications which make best use of the capabilities of the Raspberry Pi.

Installation

To get the latest version on your Raspberry Pi, first check the Gluon download page for the download link.

Gluon JavaFX download page

Copy the link and use it with wget to download the file:

  1. Download the file
$ wget -O openjfx.zip https://gluonhq.com/download/javafx-linux-arm32-drm-sdk/
  1. Unzip the file
$ unzip openjfx.zip
  1. Move the unzipped directory to the opt-directory (optionally, but it’s a logical place)
$ sudo mv arm32fb-sdk/ /opt/arm32fb-sdk/

Start an application

Now the OpenJFX-runtime is available on our Raspberry Pi, we can start each Java application which was compiled to a JAR with some additional parameters to run it with the best rendering support.

The additional arguments are needed to link to the downloaded JavaFX library and select the correct Monocle platform.

java \
  -Dmonocle.egl.lib=/opt/arm32fb-sdk/lib/libgluon_drm.so \
  -Djava.library.path=/opt/arm32fb-sdk/lib \
  -Dmonocle.platform.traceConfig=false \
  -Dprism.verbose=false \
  -Djavafx.verbose=false \
  -Dmonocle.platform=EGL \
  --module-path .:/opt/arm32fb-sdk/lib \
  --add-modules javafx.controls \
  --module {YOUR_MAIN_CLASS} $@

TODO: describe each of the startup arguments, here or in a sub page.

Minimal example application

In this repository https://github.com/Pi4J/pi4j-example-javafx, a minimal example project is provided which combines Java, JavaFX, Pi4J and a few buttons. The wiring for this example is identical to the Minimal example application with a single LED and button.

To test the application:

  1. Download the sources to your Raspberry Pi
$ git clone https://github.com/pi4j/pi4j-example-javafx
  1. Move to the downloaded directory
$ cd pi4j-example-javafx
  1. Build the project
$ mvn package
  1. Move to the target > distribution directory
$ cd target/distribution
  1. Run the application with the provided run-script
$ sudo ./run.sh