Friday, November 02, 2007

JavaFX in Eclipse: mini-HowTo

I've been meaning to explore JavaFX for some time now, and decided to spend a couple of days on it this week, as part of an investigation for an ongoing semantic web UI project. My Java development environment is Eclipse, and since I had to jump through a couple of hoops to get JavaFX working in Eclipse, I thought I'd document the steps here.

  1. Install JavaFX locally. I downloaded the .tgz archive from the OpenJFX site. Beware that there's no top level directory in the archive, so create a directory first, for example /usr/share/java/openjfx, and cd there before un-tar'ing. I haven't checked that the .zip file has the same issue for Windows users, but I'm guessing it does. I'll refer to this directory later as $JFX.
  2. Install the JavaFX plugin for Eclipse in the usual way from this auto-update site:
    (see also step-by-step instructions)
  3. Create a new Java project (there's no JavaFX-specific entry in the New context menu for new projects, but that's OK). In the source folder, create a new JavaFX file from the New context menu (right-click » New » Other » JavaFX » JavaFX file).
  4. Add some source code to the file. My test file is, imaginatively, test1.fx. I added one of the standard tutorial examples:
    import javafx.ui.*;
    Frame {
        title: "Hello World JavaFX"
        width: 200
        height: 50
        content: Label {
            text: "Hello World"
        visible: true
  5. Note the sytax error: Frame is not recognized (well it wasn't for me, anyway). Solution: add the JavaFX libraries to the build properties. The plug-in adds a library to the User libraries of Eclipse: I guess that in future releases the library will be automatically added. Also, if your downloaded copy of JavaFX is newer than the plugin's, you could reference the libs in $JFX/trunk/lib here.
    Update: Don't remove JavaFX and add a different library (for example JavaFX-local) that references different versions of the Java fx lib .jars. It turns out that the current version of the plugin will automatically add a JavaFX library if there's not one in the build path when you try to launch the app. Having two sets of JavaFX jars makes things break in an ugly way. I've reported this as a bug.
  6. To run the example, there's no 'run as JavaFX' from the context menu of test1.fx. Instead, click Open Run Dialog..., create a new entry under JavaFX Application, and enter the name of the fx file to run as an argument to the JavaFX runner:

    Save the run config, then run the application. javafx, java, eclipse, howto


Anonymous said...

It seems there are no files at the mentioned URL i.e.

Any help where can these files be found now ?

Ian said...

Hi Ankit,
My mini-howto is 9 months old now, and JavaFX has moved on quite a bit. I'm not surprised that my post is now out-of-date. However, I'm not currently focussing on JavaFX technology in my project work, so I've lost touch a bit with the state of the art. Google says that there are other, newer, instructions for working with JavaFX and Eclipse. I suggest you investigate one of those. Good luck!


Anonymous said...

Thx, nice "how to", helped me a lot!
carpe noctem