Tools

JavaFX Database Programming with Java DB

By Eric J. Bruno, April 09, 2010

Who said JDBC programming with Java and JavaFX Script was difficult?

Eric Bruno is a contributing editor to Dr. Dobb's and can be contacted at eric@ericbruno.com

.

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.

[Click image to view at full size]

Figure 1: The Plugins window lets you add new features (such as JavaFX development) to NetBeans.

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.

[Click image to view at full size]

Figure 2: With NetBeans plug-ins, there's support for multiple programming languages. Now you can add JavaFX to the list.

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.

Example 1: A basic JavaFX application created by the NetBeans New Project wizard.

Stage is analogous to a window frame, title bar, and other decorations. You can customize these, or turn them on or off, by manipulating properties such as title, as shown in this code. The Scene represents and contains the content within the window, as well as the overall window size. In this example, the content is a text label which itself has the words Application content as its content. There are many JavaFX controls to choose for additional 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.

[Click image to view at full size]

Figure 3: The code from Example 1 results in this application window when run.

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.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!