Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Practical JavaFX 2, Part 2: Refactoring Swing JPad's basic UI features to JavaFX

Migrate JPad's content pane, menu system, and event handling to JavaFX

  • Print
  • Feedback

Upgrading a Swing-based app to JavaFX gives you access to the modern UI features of a RIA framework, as well as the ability to easily deploy your application in multiple environments. In this article, you'll see for yourself how a Swing-based notepad application's basic UI architecture maps to JavaFX. There are some adjustments involved, but the trade-off in rich UI features and flexible deployment could be worth it.

The first part of this article introduced a Swing-based notepad application. Now I'll introduce its JavaFX counterpart, JPadFX. We'll start with the basic UI architecture of JPadFX, which maps to the features developed for the Swing JPad: border pane, menu system, scene, and stage. I'll also discuss observable lists, which don't map to a Swing counterpart. Finally, I'll introduce JPadFX's event-handling architecture and explain how it differentiates from the approach used in the Swing-based app.

Note that Part 2 focuses on the basic, and essential, features of a JavaFX notepad application. Part 3 introduces the more advanced features of dialog boxes, the clipboard, and drag-and-drop support.

Introducing JPadFX

JPadFX is the JavaFX equivalent of the Swing-based JPad application that I introduced in the first part. JPadFX is more or less identical to the Swing-based notepad, as you can see in Figure 1, a screenshot of JPadFX's user interface.

Figure 1. A screenshot of JPadFX's UI (click to enlarge)

 

Note that unlike the Swing-based notepad, JPadFX shows no undo function on the UI. JavaFX doesn't support an undo function, and I chose not to introduce a legacy UI dependency into the application. As JavaFX matures the need for such dependencies (or doing without them) will decrease.

Compiling and running JPadFX

You can download the source files for JPadFX anytime: JPad.java, About.java, Alert.java, AreYouSure.java, and the resource file icon.png. After extracting these files to your current directory, execute the following commands to compile JPadFX.java and the other source files, and run JPadFX. The third command tells JPadFX to open and display the contents of JPadFX.java:

javac -cp "C:\Program Files\Oracle\Javafx 2.0 SDK\rt\lib\jfxrt.jar";. JPadFX.java
java -cp "C:\Program Files\Oracle\Javafx 2.0 SDK\rt\lib\jfxrt.jar";. JPadFX
java -cp "C:\Program Files\Oracle\Javafx 2.0 SDK\rt\lib\jfxrt.jar";. JPadFX JPadFX.java

Note that the above commands assume a Windows platform and that you have installed the JavaFX 2.0.2 SDK to C:\Program Files\Oracle\JavaFX 2.0 SDK. This home directory's rt subdirectory contains a lib subdirectory. That subdirectory contains jfxrt.jar, which in turn contains JavaFX API class files. I've appended a dot (representing the current directory) to javac's classpath so that this tool can locate the other source files.

  • Print
  • Feedback

Resources

More from JavaWorld

  • Find more of Jeff's writing in Java Tutor, his blog on JavaWorld.
  • See the JavaWorld Site Map for a complete listing of research centers focused on client-side, enterprise, and core Java development tools and topics.
  • JavaWorld's Java Technology Insider is a podcast series that lets you learn from Java technology experts on your way to work.