JavaFX: An Overview



Java has always provided APIs for building applications with graphical user interfaces: Abstract Window Toolkit (AWT) and Swing are common examples. JavaFX is the latest. It was intended to replace Swing by making it easier to develop sophisticated GUIs. JavaFX has been around for quite some time, and is seeing a steady rise in adoption; however, Swing is still widely used.

JavaFX is baked into the JRE and JDK; hence, it requires no “special” programs to use. JavaFX applications are cross-platform, and can run on the browser, embedded devices, and mobile. Gluon Mobile by Gluon makes it possible to create JavaFX apps that run on both Android and iOS from a single code base.

Learning JavaFX is quite easy, thanks partly to the theater analogy built into the naming. Think of a JavaFX Stage like a theater stage. As a play progresses, scenes are changed; that’s essentially what happens in a JavaFX app. The Stage is the main layer, and the actions all take place in the various scenes. To build a JavaFX app, all you need to do is to extend the Application class from javafx.application and override the start method.

Of course, creating UI elements like this (exclusively with code) doesn’t usually scale. JavaFX allows FXML, an XML-based markup language used in defining the UI components. Since FXML is not compiled, changes are live. This decouples the components that make up the application, making it possible for development to follow the MVC architecture. Additionally, you don’t have to bother about application code. Plus, localization is made easier.

If there’s one thing that makes developing in JavaFX great fun, it’s surely JavaFX CSS. JavaFX CSS is only slightly different from CSS, particularly in its syntax: JavaFX CSS has -fx- prepended. So, -fx-background-color would translate to background-color in CSS, and achieves the same effect as in HTML. JavaFX CSS completely delegates the look and feel of controls to the user’s discretion. Each UI control—button, label, checkbox, etc—is highly customizable, from background color to opacity. In fact, the default CSS used in styling all UI components, Modena, is totally replaceable with custom CSS. (Oracle provides this complete documentation of JavaFX CSS worth checking out.)

Binding is another powerful feature of JavaFX. Imagine writing Java code that automatically recomputes the value of a sum when one variable changes—Not quite straightforward! With binding, once the relationship between the variables is established, Java takes care of the rest. Leveraging the power of binding and properties of objects, sophisticated application components can be created with fewer lines of code. Two categories of APIs are dedicated to binding: high-level and low-level. The low-level API comes in handy when the high-level API won’t suffice. The low-level API also requires some experience to use.

There are several other benefits JavaFX has over Swing, like better animations and visual effects, and web content rendering, to name a few. To make the two frameworks interoperable as well as migration-smooth, JavaFX also makes it possible to embed Swing components in JavaFX applications.

There are tons of resources to get you started with JavaFX development. JavaFX javadoc is also a good place to start. Here is a list:

  1. Javadoc
  2. Oracle Resources
  3. Oracle Developers Youtube Channel

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Bruno is a junior at Ashesi University College studying Computer Science. He is interested in leveraging the power of technology to increase productivity. As a big fan of open source technology, he is currently exploring the possibility of using the Bitcoin Blockchain to fight corruption in government.


Click on a tab to select how you'd like to leave your comment

Leave a Comment

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