Eric Bruno is a contributing editor to Dr. Dobb's and can be contacted at [email protected]
.
The promises of JavaFX include the ability to quickly and easily create Java GUI applications; run on desktops and mobile devices without changing source code; and the integration of Java code. With it, you can build media-rich applications with far less code than you would with Swing, yet you can still call into your existing Java codebase. This includes databases via JDBC as well, but there are some tricks you'll need to know, which we'll explore here.
Assume you've been asked to build a library front-end system to allow patrons to borrow and return books. The requirements include support for multiple operating systems (i.e. Windows, Mac OS X, and Linux). Java helps to solve that easily, and the addition of JavaFX solves the multimedia requirements as well (such as support for book cover photos, 30-second music samples, and video clips from movies). For our solution's UI, we'll choose JavaFX. The sample code I examine won't include the multimedia features; instead I focus on JDBC integration.
Next, to track library patrons and the books they borrow and return (hopefully), you need a database. For this sample application, I use Java DB since it's open-source, readily available with the Java Development Kit (JDK), and easy to use. Since JavaFX integrates with Java, we should be able to integrate the UI code with the JDBC code with little effort. First, let's discuss how to get started with JavaFX and Java DB development. The complete source code and related files for the Library Application is available here
JavaFX Primer
To begin, you must have JavaFX installed on your development machine. You can find the appropriate download bundle at javafx.com/downloads. If you don't have NetBeans 6.8, it's recommended you download the NetBeans 6.8 and JavaFX bundle; otherwise you can download and install the JavaFX SDK only. If you're committed to Eclipse, you can download the JavaFX plug-in for Eclipse from this page as well. The latest version of JavaFX as of this writing is version 1.2.3.
If you own a Windows Mobile device, you can download JavaFX Mobile to install and run JavaFX applications there as well. For this article, I focus on running JavaFX on the desktop.
If you already have the latest Java and NetBeans installed, you can install the JavaFX plug-in for NetBeans via the Plugins window from the Tools menu (see Figure 1). You'll still need to install the JavaFX SDK separately.

With the JavaFX Kit plug-in, NetBeans allows you to create new JavaFX projects, and the editor will support JavaFX Script development (see Figure 2). This includes syntax highlighting, code completion, in-line error detection, and source code debugging. Project properties allow you to specify how the application is run, such as with Java WebStart, standalone, within a browser, or on a mobile device. You can also specify, if you choose to deploy as a browser-based JavaFX applet, where the applet can be dragged off the browser and onto the desktop -- a convenient feature for JavaFX application installation.

You mainly develop JavaFX applications with JavaFX Script, which is an expressive dynamic language that simplifies the GUI development process. It's also meant to be a straightforward way for non-programmers (i.e. graphic artists) to design user interfaces and content to be shared with programmers. More involved logic can still be built with Java, and easily integrated with the JavaFX Script UI code. Example 1 shows a default, very simple, JavaFX application.
Stage { title: "Application title" scene: Scene { width: 250 height: 80 content: [ Text { font : Font { size : 16 } x: 10 y: 30 content: "Application content" } ] } }
You include more content in the Scene by adding more GUI components to the content property between the opening and closing square brackets, which represent an array of items in JavaFX Script. You control the layout of these items through a combination of horizontal, vertical, and grid layout containers, as well as explicitly setting the x and y placement of each component.
When compiled and run, the script in Example 1 results in an application that resembles Figure 3.

The JavaFX Stage results in the window frame and title bar with the text Application title. The Scene results in the content within the window (outlined in black), such as the text Application content in this example. Although the layout of components in JavaFX Script is straightforward, a graphical layout manager makes the task even easier; this is where the JavaFX Composer tool comes in.