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
- Netbeans is the IDE which is the most advanced and usable with JavaFX 2
- You want Netbeans 7.1 RC2
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:controllerattriubte, which defines the Control for this View
- FXML elements are bound to members contained in the controller class which have been annotated with the
fx:idproperty 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
onActionhandler, like this:
#methodNamesyntax 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.
Related Articles :