What I Learnt about JavaFX Today
Join the DZone community and get the full member experience.Join For Free
In case you haven’t heard, JavaFX 2 is the new Desktop / web / client framework for Java. It’s had a considerable overhaul since JavaFX 1 (which was frankly not that impressive). Out has gone the custom scripting language, and instead you can write it using standard Java and an XML-based language for the actual UI presentation.
So today, a friend and I got together at one of our places to teach ourselves a bit of JavaFX. Here’s what we learned, starting with some of the yak-shaving we had to do:
- First of all, install the JavaFX developer preview – get it here
- You have to unzip it, and place the resulting directory somewhere sensible, chown’d to root.
- I put it in /usr/local/javafx-sdk2.1.0-beta/
- Next, you’ll want an IDE to go with that
- Netbeans is the IDE which is the most advanced and usable with JavaFX 2
- You want Netbeans 7.1 RC2
- To get this to install on a Mac, you need JavaForMacOSX10.7.dmg – no lower version of official Apple Java will do, and an OpenJDK build won’t work either (even if it’s the correct version or higher)
- Once it’s installed, Netbeans will work fine with other JREs (I was mostly running it against the Java 7 Developer Preview)
- To start new JavaFX projects, you need to tell NetBeans where to find JavaFX. For this, you need to create a new JavaSE platform profile, and add the JavaFX dependencies in manually.
Once it was installed, we started working with JavaFX properly. Our project for the day was to try to replicate some of Victor Grazi’s concurrency animations in JavaFX – both to teach ourselves the JavaFX technology, and also create some teaching tools as outputs.
- JavaFX uses Application as the main class to subclass
- The API docs are here
If you’ve done any Flex development, JavaFX will seem very natural. E.g.
- The FXML file provides the UI and layout
- The top level FXML element has a fx:controller attriubte, which defines the Control for this View
- FXML elements are bound to members contained in the controller class which have been annotated with the @FXML annotation
- The fx:id property is used to define the name of the member that is being bound to the FXML element
- Binding also occurs to methods. E.g. buttons bind use an onAction handler, like this: onAction="#isFutureDone"
- The #methodName syntax is used to say which method should be called when the button is pressed.
From this it’s very easy to get started with building up a basic application. Some things that we found:
- The UI thread can be quite easy to tie up. Don’t ever call a blocking method directly from the Control object, as triggering this code path on the UI thread will cause the display to hang.
- Be careful of exception swallowing.
- If you have a method in an object which is updating a UI element, but which is not annotated with @FXML, then you seem to need to call requestLayout() on the UI element after updating it. We’re not sure we got to the bottom of why – please enlighten us if you know why.
- The framework seems to use custom classloading to transform the FXML file into a “scene graph” of objects, seemingly a bit like how Spring does it.
On the whole, we were quite impressed with our short hack session. The APIs seem clean, and the overall design of the framework seems sound. There were a few stability issues, but this is bleeding-edge tech on Mac – both the JDK and the JavaFX runtime are Developer Previews.
We’ll definitely be back to do some more with JavaFX, and look forward to seeing it mature and become a fully-supported OSS framework for client development in Java.
Opinions expressed by DZone contributors are their own.