Category

Running your first JavaFX program in Eclipse

There is a plugin for Eclipse for developing JavaFX. The official installation instructions are
ok to a point. You do get the plugin installed. But then what? I hope to save you a bit of time with this post.

Create a new Java project. You don’t have to add anything to the classpath.

I have a preference set to create the src/bin folders for my java projects. If that is your preference, create your JavaFX files in src. If you are using the project as the source root, then put them there. If you create a package, put them in there. Just like regular Java files. So, how to create them?

Select your project (or project/src) in the package explorer and open the New menu. (control-n). There is a category for JavaFX and one entry under it to create a JavaFX file. Check the parent folder is correct (that’s why you selected the project in the first place) and change File1.fx to whatever you want like YoWorld.fx.

So what happened? You get a nice empty file. No boilerplate code to delete 🙂 You also get your classpath set correctly for JavaFX. Take a look at your project’s classpath to verify.

Type in your script. Notice that command line completion works. Here’s a simple one.
(I live 9 miles from South Philly)

YoWorld.fx

Make a syntax error and you get the familiar red Xs – just like Java errors.

Now what? How do you run it?
The hard way: Create a new Java run configuration.
Run->Open Run Dialog
Choose Java Application and press the New button
Set net.java.javafx.FXShell as the main class.
Choose the arguments tab and type the name of your script e.g. YoWorld

Ok, that works. But for each script you need to change the script name in the argument tab.
A bit of a pain.

Here is an easier way:
Run->Open Run Dialog
Choose JavaFX Application and press the New button. Name it something like JavaFX 🙂
What you gain with this is you don’t have to remember that the main class is
net.java.javafx.FXShell
Choose the arguments tab.
Now under Program Arguments do not type the script name. Instead press the variables button right under the text area. I use resource_name. Choose that and when you press OK you should see just ${resource_name} in the text area. Press Apply and OK.

Now, in Package Explorer View choose your script. Go to the Run menu and choose your launch configuration. It should run. Choose another script. That script should now run. Choose nothing (e.g. click on the project). You should get an error dialog.

Extra convenience: edit your run configuration again and choose the Common tab. You can
have your JavaFX run config as a favorite at the top of the Run menu by choosing Display in Favorites Menu->Run.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.