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 1: Architecture of a Swing-based notepad

How will Swing JPad's UI features map to JavaFX 2.0?

  • Print
  • Feedback

With the release of JavaFX 2.0 the question for many Java developers is whether it's time to start transitioning Swing applications to this new UI paradigm, and what better way to find out than by a hands-on exercise? In this three-part tutorial, Jeff Friesen walks through the process of refactoring an example Swing-based notepad editor application to its JavaFX 2 equivalent.

JavaFX 2 is a user interface library and platform for creating and running modern Java application UIs that feature rich graphics, animation, media, controls, and more. It's also a toolkit for deploying JavaFX applications, meaning any Java application that features a JavaFX UI. A JavaFX application can be deployed standalone, launched via Java Web Start, or embedded in a web page.

This article serves as a practical introduction to JavaFX 2, and is geared to developers experienced with Swing-based user interface (UI) development. By refactoring a Swing application, you'll familiarize yourself with some of the most useful features of JavaFX 2. You'll have the opportunity to try it on for size. And you'll get some practice in refactoring Swing apps to JavaFX -- which could come in handy if JavaFX 2 turns out to be the next-generation Java development platform that Oracle clearly hopes it will.

Oracle has been pushing JavaFX 2 as the next-generation platform for Java, but does that mean it's time to jump from Swing to JavaFX? Refactoring a Swing demo app to JavaFX is one way to find out.

This first part of the article is an overview of a Swing demo application, a text editor called JPad. Experienced Swing developers can probably give the architecture a glance, then move on to refactoring the app for JavaFX in Part 2 and Part 3. For those less experienced with Swing, Part 1 offers valuable insight into client-side application development, which you'll probably need in order to follow the refactoring examples.

JavaFX 2 release history

Oracle released JavaFX 2.0 beta in May 2011 and declared it production-ready in October of that same year. The most recent version, JavaFX 2.0.2, was released in December 2011 with some bug fixes and minor features added. Examples in this article are based on JavaFX 2.0.2.

Let's start with a review of new features in JavaFX 2, some of which you'll encounter in the refactored JPad architecture.

Big changes in JavaFX 2

JavaFX 2 differs significantly from previous versions, and one of the biggest changes is the deprecation of JavaFX Script. JavaFX developers are free to program in Java code, and may also experiment with JVM scripting languages such as Groovy or Scala. Java code is more verbose than JavaFX Script, but there's hope that Java 8's support for lambda expressions in the Java language will bring improvements.

The following JavaFX Script language features have been replaced with Java API equivalents:

  • 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.