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:

  1. First of all, install the JavaFX developer preview – get it here
  2. 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/
  3. 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
  4. 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)
  5. Once it’s installed, Netbeans will work fine with other JREs (I was mostly running it against the Java 7 Developer Preview)
  6. 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.

  1. The FXML file provides the UI and layout
  2. The top level FXML element has a fx:controller attriubte, which defines the Control for this View
  3. FXML elements are bound to members contained in the controller class which have been annotated with the @FXML annotation
  4. The fx:id property is used to define the name of the member that is being bound to the FXML element
  5. Binding also occurs to methods. E.g. buttons bind use an onAction handler, like this: onAction="#isFutureDone"
  6. 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:

  1. 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.
  2. Be careful of exception swallowing.
  3. 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.
  4. 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.

Did you like this? Share it: