JavaFX 8 with Eclipse Mars

JavaFX 8 is the coolest thing since sliced bread. After installing the 2015 Mars release of Eclipse, the first question I asked was, “Where’s JavaFX 8?”

Don’t worry. Installing the right tooling for Eclipse Mars to develop JavaFX 8 apps is not difficult at all if you download the pre-setup version here. However, if you want to add the JavaFX 8 tooling to an Eclipse that doesn’t have the JavaFX 8 tooling already, things get a little more involved. But still, it really isn’t difficult.

This tutorial focuses on getting JavaFX 8 up and running with Eclipse Mars. For an alternate JavaFX 8 Hello World introduction, see my other tutorial. In that tutorial, I skip most of the instructions on getting the JavaFX 8 tooling working with Eclipse and go straight to the good stuff, like making a JavaFX 8 application using FXML.

Setting up JavaFX 8 with Eclipse Mars

To develop JavaFX 8 projects on Eclipse, you need to use the e(fx)clipse tooling. You can use the standard approach of pulling it automatically from a repository link, but you have to add the link by hand. The Eclipse Marketplace only has older versions of e(fx)clipse, currently. If you tried installing e(fx)clipse from the marketplace, and failed miserably, that would be the reason. Instead, follow these steps.

  1. Make sure that you are using JDK 8. In other words, check that your Java compiler compliance level is a minimum of 1.8. JavaFX 8 requires (surprise, surprise) Java 8.
  2. Navigate to Help->Install new software …
  3. You should now be looking at the Install window. Click the Add… button.
  4. You should now see the Add Repository dialog. In the Name textbox, type e(fx)clipse. In the Location textbox, paste
    http://download.eclipse.org/efxclipse/updates-released/2.0.0/site
    and click the OK button.
  5. The Install window should now display the check box labeled e(fx)clipse – install. Select that checkbox and follow through with the installation. It probably involves a couple of Next button clicks, a Finish button click, and allowing Eclipse to restart.

This installation process works for both Eclipse Java edition and Eclipse JEE edition. JavaFX works great as a GUI for traditional standalone applications, just like Swing and AWT before it did. However, JavaFX 8 has great features for integrating it with HTML5 and JEE based applications. That means that being able to install e(fx)clipse onto an Eclipse JEE makes a lot of sense. You can replace your typical Web browser with a thin client that handles HTML5 just as well as Chrome or IE, but also can have specialized menus, dialogs, and other features unique to your application instead of the web browser specific menus and dialogs. (Obviously, JavaFX 8 also makes a great platform for creation of a thick client, too.)

Now that you have JavaFX 8 installed on standard Eclipse or Eclipse JEE, it’s time to validate your setup. You want to see your first JavaFX 8 project, along with a JavaFX 8 application running.

You create your JavaFX 8 project in a similar way as you create a standard Java project. Navigate to File->New->Other…

Next, scroll down to the JavaFX folder, expand the folder, and select JavaFX Project from the long list of JavaFX 8 goodies that it lists. Creating the project from that point is pretty much the same as for any standard project.

Your shiny new JavaFX 8 project should appear in the Eclipse Package Explorer. Right-click and run it as a Java application, just like you would with a normal Java project. After running your JavaFX 8 project, you will see an empty window.

That window is your JavaFX 8 Stage. JavaFX 8 applications are organized as stages that contain scenes. Scenes have groups and terminal nodes (also called leaf nodes.) Groups and scenes are containers, such as the AnchorPane. Groups may be nested in other groups when creating complex UI’s. Terminal nodes are buttons, labels, textfields, drawn shapes, or other non-container GUI elements.

Scenes can be swapped out on the stage without adversely affecting your window. Those familiar with AWT and Swing may remember the nightmares that occurred if you tried frequently swapping Panes in and out of your window or frame in those APIs.

Anyway, you now have a fully functional JavaFX 8 development environment for Eclipse Mars. There are tons of different approaches to developing your JavaFX 8 UI. The newest involves SceneBuilder, FXML, and dependency injection. JavaFX fully embraces MVC/MVP in a way that neither Swing nor AWT ever could.

Be sure to check out my other JavaFX 8 tutorials and examples. I’m planning to write a lot more, and I’ve already written quite a few.