As of JavaFX version 1.2, Sun Microsystems the name (will be Oracle by the time you read this) officially released a fully functional plugin to support development in the Eclipse IDE. While the Eclipse plugin came after NetBean's, it still packs an invaluable set of functionalities for developers who feel more comfortable working in Eclipse, including:
Project creation wizard and templates
Syntax highlighting
Code completion
Error detection
Code block formatting and folding
In-editor API documentation
Debugging
Continuous background build
And more…
This recipe shows how to set up the Eclipse IDE for JavaFX development. You will learn how to configure Eclipse and the JavaFX 1.2 plugin.
Before you can start building JavaFX applications in the Eclipse IDE, you must ensure that your development environment meets the minimum requirements for JavaFX 1.2, which requires Eclipse 3.4 (Ganamede) for Java EE developers (or higher). To get the Eclipse plugin to work properly, ensure that you have downloaded and configured the Java JDK and the JavaFX SDK (see the recipe Installing the JavaFX SDK for details).
As with anything else in Eclipse, JavaFX support comes in the form of a plugin. You have to download and configure the plugin to work with your previously installed local JavaFX SDK prior to building your applications. To get started, do the following:
1. Select Software Updates from the Help menu to open the Plugins management window.
2. Click on the Available Software tab.
3. Add the site http://javafx.com/downloads/eclipse-plugin/ as the plugin site.
4. Select the JavaFX site, as shown in the previous screenshot, then click on Install to continue.
5. Follow the instructions of the plugin wizard.
6. Accept the terms of the license, and make sure to restart the Eclipse IDE when prompted.
Now that you have Eclipse setup with JavaFX, it makes sense to create a quick Hello World application in Eclipse to test the installation.
To get started, select New from the File menu (you may have to select Other if JavaFX is not listed as a project type).
Note
You must have the SDK installed and configured prior to creating your first application (see recipe Installing the JavaFX SDK).
7. When presented with the new project wizard, select JavaFX Project and click on the Next button.
8. Then, provide the project's name (HelloWorld), location, JRE version, and type (default is Desktop) to continue with the project's creation.
9. Click on the Next button and select a project template (which is a based on pre-existing sample code). Select the Empty Project template and click on the Finish button.
10. The wizard will complete the project creation, and you should have a project shell ready for you to start coding.
11. To continue, create a new code package (right-click on the project source directory, and select New | Package), and name the package
hello
.12. Next, right-click on the newly created source package and select New | Empty JavaFX Script from the context menu, and name it
HelloJavaFX.fx
.13. This will do exactly what it says, which is to create an empty code window. Notice, however, that the editor comes with several code snippets that you can reuse in your own code.
14. In the Snippets window, shown in the previous screenshot, click on Applications and double-click on Stage. This will bring up a template editor. Accept the default values and continue.
Edit the sample code by adding the highlighted portion. You can see the full code listing at ch01/source-code/src/hello/HelloJavaFX.fx
.
import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.text.Text; import javafx.scene.text.Font; Stage { title: "Hello JavaFX" scene: Scene { width: 250 height: 200 content: [ Text { font : Font {size : 16} x: 10 y: 30 content: "Hello World!" } ] }
Once you have updated the code, right-click on the project and select Run As | JavaFX Application. If you are running the application for the first time, you will be prompted to select the application's targeted profile and the main class.
Support for JavaFX in Eclipse comes as separate plugin download. When you install the plugin, it adds the capabilities of JavaFX development to your IDE. Unlike the NetBeans plugin, as of version 1.2, the SDK is not available as part of the plugin download. You must download and have the SDK installed on your workstation. When the plugin is installed, it will look for the SDK on your machine.