Adverts
There is both a NetBeans and an Eclipse plugin for JavaFX. The NetBeans plugin seems to work pretty well the Eclipse plugin on the other hand is difficult to work with. This page describes my efforts to get the Eclipse plugin working. To install the plugin you need to be running at least Eclipse 3.2.2 and Java 5.0 or above. The page seems to indicate that it will only work with Java 5.0 but I've had success with Java 6.0 as well. Follow the instructions that are found at the openJFX plugin page to install the plugin.
Once the plugin has been installed start a new standard Java project in Eclipse and then create an "examples" package under source. In this package create a file called "example.fx" in the same way that you would create a class file.
In the "example.fx" file add the code:
import javafx.ui.*;
var win = Frame {
title: "Hello"
width: 300
height: 300
content: TextField {
value: "Hello"
}
visible: true
};
Right click on the file and select "Run As > Run...". Select JavaFX application and set up a new run configuration called Standard. This is where the first problem occurred. The automatic inclusion of the JavaFX libraries didn't happen so the run time didn't have access to any of the jars needed to run a JavaFX application. In the classpath tab add three user entries "swing-layout.jar", "Filters.jar" and "javafxrt.jar" which can be found in the plugins directory of eclipse. Press the "Run" button" to run the application. At this point a window should appear but on my system the application appears to run but no window pops up. I'm fairly sure the application is starting because I get this message in the console:
compile thread: Thread[AWT-EventQueue-0,6,main] compile 0.0 init: 0.031
Which to my eye looks like it worked. Where the window is though is anybodies guess. I made a step forward at this point by guessing that the run configuration for JavaFX applications wasn't correct. JavaFX applications are started by passing them to the runtime environment which is "net.java.javafx.FXShell". The problem is getting Eclipse to pass the correct path to the FXShell application so that it starts the FX application. I decided that it would be a good idea to drop to a command line at this point and try it from there. I got the application running using the following command:
java -cp .;c:\javafxlibs\javafxrt.jar;c:\javafxlibs\Filters.jar;c:\javafxlibs\swing-layout.jar net.java.javafx.FXShell example
Move to the same directory as the fx file in order to execute it. There are two things to remember: Firstly you have to add . to the class path as the example.fx file is loaded as a classpath resource and you are in the same directory as the fx file, secondly you need to all all three jar files to the class path. This makes sense though as you are loading a swing application. The application loads in about 3 seconds on my machine which is fairly moderate. The output shown above is exactly the same whether the load works or fails - only the load time is different. It would be nice if FXShell complained if it wasn't passed a file to execute. Now on to trying to configure Eclipse to do the same thing.
I finally cracked the problem by adding the example source directory to the classpath. To recap then: After creating the "Standard" JavaFX Application run configuration add the three JavaFX jars for the "User Entries" section and also add the example package under the source folder. Thinking about it this makes sense when you consider that the fx file is loaded as a classpath resource. It's just a shame that the plugin doesn't do this for us. You also need to include the name of the fx file you want to run in the arguments section of run configuration or the FXShell won't know what you are trying to run.