Making your life as a developer easier

Taking a closer look at JavaFX

© Shutterstock / Imaake  

JavaFX is now part of the Java SE 8 SDK. In this article, Java Champion Johan Vos gives us a quick overview of this essential framework and explains why you should use JavaFX instead of Swing.

The JavaFX framework contains API’s that allow developers to create user interfaces using the Java language. The JavaFX packages reside in the javafx namespace, and the framework is part of the Java SE 8 SDK.

JavaFX was announced during the JavaOne conference in 2007. Before JavaFX, the standard API’s for creating user interfaces in Java were the AWT and Swing API’s. The AWT API’s where part from the platform since the very beginning of Java (JDK 1.0, 1996) and the Swing API’s have been added in JDK 1.2 (1998).

Clearly, the UI technology has changed a lot between 1996 and now. In 2007, Sun Microsystems decided it was time for a new set of API’s that target UI development in the Java platform. It took a few iterations before the API was crystallized, and in 2011 JavaFX 2.0 was released. Between 2011 and now, more features have been added, and the JavaFX API’s are now part of the Java SE 8 SDK. The JavaFX code is developed in the OpenJFX repository, which is aligned with the OpenJDK repository where the core Java SE API’s are being developed.

The all-new JavaFX

There are a number of specific design elements that make JavaFX different from its predecessors (AWT and Swing). Those elements combined make the life of a developer easier, and align the development of user interfaces with Java development in general. Developers should not be bothered too much with low-level and performance details, while still be allowed to describe in a very granular detail how a user interface should look like.

JavaFX uses a scene graph to organize graphical objects. A scene graph provides a tree structure consisting of nodes. A node can have specific rendering instructions, and it can also contain child nodes. The scene graph approach is very popular today in vector-based rendering applications.

SEE MORE: JavaFX and Near Field Communication on the Raspberry Pi

JavaFX is a modern UI framework that uses retained rendering instead of immediate rendering. In retained rendering mode, the developer doesn’t change the low-level graphics themselves. Rather they supply hints to the rendering engine (e.g. add a button, animate between here and there, etc.) and the rendering engine is then responsible for ultimately rendering the pixels on the screen, taking into account the requests from the developer.

The low-level JavaFX rendering system allows for a number of pipelines to do the real work. Wherever possible, hardware acceleration is used. On Windows, Direct3D is used. On most other systems (Linux, Mac, iOS, Android, embedded ARM) OpenGL is leveraged. Thanks to this hardware accelerated rendering, lots of work is offloaded from the CPU to the GPU. This allows developers to create applications in JavaFX that are very performant, and that benefit from GPU’s to become increasingly better.

Dynamic binding of user interface controls

JavaFX introduces the concepts of Observable and Binding. Although these are not UI-specific, they are particular useful when dealing with user interfaces. Implementations of the JavaFX Observable interface allow other objects to register interest in modifications to the Observable object. This concept is used for the dynamic binding of user interface controls to data. For example, the JavaFX ListView control is backed by an ObservableList (where ObservableList extends Observable). If elements are added do the backing ObservableList, the ListView will immediately render these as well (provided they are expected to be in the visual part of the screen).

When well written, JavaFX applications behave much better in environments with multiple CPU’s or cores. A dedicated thread is used to make changes to the scene graph. All other computations are recommended to be executed on different threads. This approach allows developers to make sure that the scene graph is always in a consistent state, while exploiting the multi-core architecture or modern devices as much as possible.

Moving on from Swing

Swing, the predecessor of JavaFX, was very popular for developing intranet applications that had to be executed on desktop systems. Many large organizations have complex Swing applications that allow their employees to perform business-related functionalities. Typical vertical markets include the financial industry, healthcare and telecommunications. Swing was the default Java UI framework for a very long time, so it is not surprising that many of these applications are very complex. They started a long time ago, and functionality has been added over the years.

SEE MORE: A fresh breeze by Enterprise JavaFX

Many companies are now seeing the benefits of JavaFX, and are migrating their existing Swing applications to JavaFX applications. While there is definitely a learning curve for JavaFX, many developers are relative fast up to speed. JavaFX API’s are very well aligned with the other API’s that are part of the Java SDK.

JavaFX on your phone

Another reason to use JavaFX is the fact that applications are portable to mobile devices as well. The most popular mobile environments (Android and iOS) allow for an OpenGL based rendering as well. The JavaFX code in the OpenJFX repository works on those environments too. Combined with the Gluon Mobile tools, developers can easily run their JavaFX applications into native apps for Android or iOS, and upload them to the Play Store and the AppStore.

As a consequence, applications are not only accessible via desktop systems, but also via the growing number of mobile devices that are increasingly become popular to end users.

In conclusion

In the early days, the JavaFX API’s were rather volatile. However, since JavaFX is distributed with the Java 8 SDK and beyond, the API’s are mature and stable. Backward compatibility is extremely important to the Java language. As a consequence, developers should not expect much experimentation in the official JavaFX API’s.

There are a number of third party initiatives like ControlsFX and JFXtras that contain advanced or specific UI controls and widgets that can be very useful to developers. The JavaFX community contains a wide number of active members who are willing to contribute their ideas and code. The stability and maturity provided by the official JavaFX distribution, combined with this enthusiasm and experiments from community members make JavaFX a great choice for UI development for Java developers.

Johan Vos started working with Java in 1995, as part of his PhD research. He joined the Blackdown team and ported Java 1.2 to Linux/SPARC. Johan has been a Java consultant ever since, and worked for a number of companies (e.g. The Reference, Acunia). He co-founded LodgON, where the main focus is on social networking software and recently became a Java Champion. He was part of the Core Platform Expert Group of OSGi that created the OSGi platform specifications. His main technology interests are currently GlassFish and JavaFX. He holds an Msc in civil engineering (mining) and a PhD in Applied Physics.

comments powered by Disqus