What is JavaFX and why do we need it?

JavaFX is a framework to build applications with modern graphical user interfaces (GUI) for computers, phones, tablets, embedded devices, TVs, kiosks, and more.

You can use Java programming language to build applications with JavaFX, but you’re not limited to Java. You can also use other programming languages like Clojure, Kotlin, Scala, Ruby, Python, and JavaScript.

An application (or app) is basically a piece of software that users interact with. So we had apps way before the birth of JavaFX in the 2000s, so why would we use JavaFX now?

Glad you asked.

JavaFX is fast

JavaFX performance

Xerox, today’s printer company, was actually much more than a printer company.

They kickstarted the information age we live in today by inventing revolutionary technologies like the personal computer, computer mouse, computer-generated imagery, and graphical user interface.

The idea of mimicking real-life controls like buttons and knobs on the screen pixel-by-pixel, then letting the users interact with them using an electronic pointing device like the computer mouse worked well for decades.

However, screens grew bigger and denser over the years. Computers’ central processing units (CPU) started to be tasked with drawing more pixels than ever.

This task, now referred to as software rendering, isn’t fast enough to handle the modern, ever-changing, animated user interfaces on high-definition screens we use now.

A different approach called hardware accelerated rendering addresses this efficiency problem by utilizing a special hardware called GPU, graphics processing unit.

Instead of constantly drawing every visual element pixel-by-pixel; GPU can take already drawn pictures or generate new ones on the fly, store them in its memory, make them visually move, add visual effects to them, then finally send the whole result to the computer monitor as fast as possible.

You can imagine hardware accelerated rendering as a tabletop where you can move playing cards quickly.

Software rendering equivalent of this analogy would be drawing all the cards on a blank sheet, on every  move.

Even though many optimization techniques were introduced to speed up GUI libraries that primarily use software rendering, they could never meet the performance expectations to handle modern apps we see on modern devices today.

JavaFX is built from scratch with high performance hardware accelerated rendering in mind.

JavaFX directly talks to the GPU via a suitable application programming interface (API) based on the platform: DirectX on Windows, and OpenGL on Linux, Android, iOS and macOS. Support for other APIs like Metal and Vulkan might come in the near future.

JavaFX is also able to fall back to software rendering if needed.

Everyone is an actor in JavaFX

JavaFX scene graph

JavaFX uses a data structure called scene graph to manage all the GUI elements.

You, as a programmer, only worry about how do your buttons, sliders, images, text, etc. look and behave on the screen.

All the complicated rendering and timing mechanisms are handled by JavaFX under the hood.

Metaphorically, on the stage, the user can watch a scene where actors move and talk. Except, JavaFX calls an actor a node.

From the programmer perspective, the application window is a Stage object, the state of the GUI is a Scene which holds the Node objects that represent the visual elements in the aforementioned scene graph.

JavaFX provides all the tools to visually alter your nodes, animate them, apply visual effects to them, and more.

Everything is reactive in JavaFX

JavaFX property and binding

JavaFX lets you connect the information together.

What if you can make the color of your room walls automatically change based on your significant other’s current mood?

Imagine your air conditioner actively reacts to your body temperature changes.

How about the sun goes down when you put your head on the pillow and comes back up when you stand up?

All of them are possible in the JavaFX universe.

JavaFX has two concepts called property and binding that let you make the state of an object change according to another object’s state based on the rules you set.

You can intuitively design your application’s graphical interface mechanics and underlying logic in a way that everything runs in sync with each other.

JavaFX is stylish

JavaFX with FXML and CSS

The looks of a JavaFX application can be implemented apart from the Java code base, coded in declarative FXML and CSS languages or using a visual design tool like Scene Builder.

This separation not only allows the creative part of your brain to fly freely like a peacock, but also lets you hire a designer without worrying them about the business logic that is programmed in Java.

FXML is an XML-based language that lets you structure your Scene similar to how web pages are coded in HTML.

CSS is a style sheet language for presenting your Scene in a visually appealing way.

JavaFX is more

JavaFX community

There are other JavaFX features that I didn’t cover in this blog post, like its 3D capabilities, APIs to deal with audio/video files and streams, and the web browser engine.

More posts are coming to show the whole picture, exploring its powerful features and advantages in more detail.

I’m also going to fill the website with practical tutorials covering every technical aspect of JavaFX and third-party libraries.

JavaFX community has been quite active. I will regularly post about the latest news, updates, announcements, and other links similar to Jonathan Giles’ blog FX Experience.

Please feel free to contact me if you have any recommendations and questions.

  1. JavaFx has been removed from Java since JDK11 which was in September 2018

  2. Correction, JavaFX binaries were removed from the Oracle JDK distribution starting with release 11.
    JavaFX _was_never_ a part of the JSR that governs JavaSE. Oracle decided (between Java 7 and 10) to bundle the JavaFX binaries with its JDK. In the meantime, distributions based on OpenJDK foe the most part _did_not_ bundle JavaFX binaries.

    The situation has changed, there are some JDK vendors (Azul, Bellsoft for example) that provide JDK distributions with and without bundled JavaFX binaries.

    In summary, one must be very careful equating "Java", "JDK", "OracleJDK", "Oracle OpenJDK builds", "OpenJDK, JDKs by vendors. They might appear like one and the same, but they are not, in the sense that there are subtle differences.