Exit Full View

JavaFX

The most obvious benefit of JavaFX is that it looks so much better than Swing. Swing has lots of different look and feels, and IMHO, that's just WRONG. Swing was designed to try to mimic different look and feels to fit in with different operating system. It failed. They all look worse than the thing they were emulating.

Don't have lots, have one good one! IMHO, it's not important to try to look different depending on the operating system. Who cares if the buttons in one application don't look the same as another application? The web is chock full of website, each with their own style, and users don't bat an eyelid.

Layout Managers in Swing are tricky, and are much improved in JavaFX. The way JavaFX is designed leads to much more intelligent design of. Look at how Tornado designs forms, and you won't want to use Swing ever again!

If skinning is important to you, then JavaFX kicks ass. (It isn't important to me, I'm happy with the out-of-the-box skin). However, using CSS to skin individual components is really easy, and really useful. With very little knowledge or research I was able to create my own custom control. I created what I called a 'Button Group' which butted up multiple regular buttons and rounding the corners of the end buttons. I was surprised that such as button group isn't available out of the box, but being able to create one so easily shows the strength of JavaFX. I wouldn't have even contemplated doing the same in Swing.

No hard coded font sizes, margins etc. These can all be specified in an application specific CSS file. This also makes it really easy to create multiple CSS files to suit different user's preferences. For example, I can create a compact and ugly skin and a hip and trendy sparse, but pretty skin. Let the user decide which they prefer.

I once tried to give create a customisable color schemes on a Swing application, and it was just horrible - I gave up! Doing the same in JavaFX it is really simple.

Binding properties in JavaFX is lovely. It took me a while to realise just how good it is. Create a property in your model, and bind it to a visual control. For example, I have a History class with "canUndo" and "canRedo" methods. Using swing, coordinating the undo and redo buttons to to become enabled/disable is a PITA, with JavaFX its a breeze.

Being able to use JavaFX for PC application AND Android applications is obviously really nice.

The Cons

There is a surprising amount of inconsistency. For example how do you add components to a container? For some ,you use an 'add' method directly, others you use children.add and for SplitPane you use getItem().add. A minor gripe.

SplitPane is broken. If you add a child to a SplitPane, there seems to be some delay (as if put it in an event queue). It sounds innocuous enough, but it means that you cannot rely on any child node being able to access getScene. This caused me huge problems, and I have a strange bodge to work around the problem: Add the children to an unseen parent first, then immediately add them to the SplitPane. For the short time that SplitPane is being lazy, the children belong to the scene graph via the temporary parent.

Convoluted implementation. While bound properties and observable lists are really powerful, they make it very hard to understand how things work. As an example, what does <code>SplitPane.getItems().add( new Label() )</code> do? I single stepped through the debugger to quite a while - its a mess, and I'm still unsure how it works (and why it doesn't set the parent immediately).

Conclusion

Don't even think of using Swing for new projects. JavaFX is far superior.

FYI, I'm using the open-source implementation, so I still don't need to touch anything proprietary from Oracle ;-)

2024 Update

I've written my own GUI toolkit similar to JavaFX, called Glok.

I needed it for two of my applications :

The first was a game engine. The game renders using OpenGL, but the game editor renders using JavaFX along. This was ok, but a huge duplication of effort. A scene must be rendered using OpenGL AND JavaFX. Some features weren't possible using JavaFX, so the view in the editor didn't look identical to the final game.

The second was a drawing program. I rendered to an off-screen OpenGL buffer, then rendered that buffer using JavaFX. This was too slow (especially when rendering with multi-samples).

My new GUI toolkit (unlike JavaFX) always uses OpenGL, and it allows the application full access to OpenGL. If the application does use direct OpenGL access, then it has to be very careful to restore the state correctly (this isn't trivial).

I'm really pleased with the result. Speed is no longer an issue.

My original plan was to create a watered down version of JavaFX, but to my surprise my final product is superior to JavaFX in many aspects. It isn't as feature-rich as JavaFX (for example there is no control capable of rendering HTML). But all the common GUI controls are there, plus many which are missing from JavaFX.

I've since ported other applications from JavaFX to Glok. Not for speed (or direct access to OpenGL), but just because I much prefer Glok! My Glok applications are easier to write and look better.

One great feature of Glok is how well it handles high DPI devices. I can scale my entire application using 1 line of code, and the change happens immediately. With JavaFX, you have to set an environment variable before the application starts. It is impossible to change the scaling factor while the application is running. How naff!

By contrast, I've all but given up using GIMP, which still doesn't work on my high-DPI laptop. They have scaled most things, but some are still the ridiculously small and impossible to use.

Another notable difference is how controls are skinned. JavaFX has two completely different approaches. CSS and Skinnable controls (I've yet to see anybody use the latter).

Glok use a DSL (domain specific language) instead of CSS. I much prefer the DSL. Yeah, I'm biased. But it is so easy to enhance the look and feel using the DSL.

All of my Glok applications let you change font sizes, accent colours, dark/light theme... It's so easy to do, that there's no reason not to! None of my JavaFX applications had these customisation features, because it was a PITA, so I never bothered.

IMHO, JavaFX's Properties are great, but not fully realised. Glok shamelessly copies the idea, but improves upon it greatly.

Glok also has a DSL, which makes creating a GUI really pleasant. Such DSLs exists as 3rd party add-ons to JavaFX. But if you are learning JavaFX, then all the documentation won't help you, as they use the (horrible) non-DSL API.