//rich client /
JavaFX and Swing Integration
Use the power of JavaFX 2.0 to migrate Swing interfaces to JavaFX.
SIMON RITTER
JavaFX 2.0 made its debut at JavaOne in October 2011 (see
“JavaFX 2.0 Is Here!” page 3).
JavaFX 2.0 is an advanced Java
UI platform for business and
enterprise applications, and it
represents the next step in the
evolution of Java as a premier rich
client platform.
JavaFX 2.0 reduces the learning curve for experienced Java
programmers because all that’s
required is learning
a new set of APIs and
gaining a grasp of use
binding, timeline-based animations, a
scene graph, and so on.
In addition, JavaFX 2.0
reverses the approach
to Swing and JavaFX
integration.
This is the first in a
series of articles that
will show how to mod-
ify an existing Swing-based appli-
cation so that the interface can be
migrated from Swing to JavaFX.
This series will show how parts of
the application can be replaced
gradually with JavaFX to improve
the appearance and usability of
the application.
plication is called StocksMonitor,
and it was created by Hirondelle
Systems. It monitors stock mar-
ket trades; displays the data in
a table; and allows users to cre-
ate, edit, and delete portfolios of
stocks. StocksMonitor is available
through javapractices.com and is
distributed under a creative com-
mons license.
are displayed on the screen.
JavaFX uses a scene graph, but as
long as we can make our JavaFX
scene appear to be a Swing component, the differences are of no
direct consequence.
Thankfully, most of the hard
work is taken care of by the
JavaFX platform. Included in the
JavaFX API is the JFXPanel class.
The name is a little misleading,
because this class extends the
Swing JComponent class rather
than the Abstract Windows
Toolkit (AWT) Panel class. As
such, we can use this easily in a
Swing application, but we can’t
use it in a pure AWT application.
JAVA IN ACTION
Important Things to Know
About JavaFX
Because JavaFX 2.0 is a set of
Java APIs, using it for a project
within an IDE is simply a matter
of including the JavaFX Runtime
file (jfxrt.jar) in the classpath.
Having done that, you
can go about developing the components
you want to use in your
combined Swing and
JavaFX application.
Note: For this series
of articles, we won’t
delve into the mechanics of JavaFX. See the
Learn More section for
JavaFX resources.
JavaFX 2.0
represents the
next step in the
evolution of Java
as a premier rich
client platform.
Integration Basics
First, let’s look at what’s required
to treat a JavaFX scene as a Swing
component. Swing uses a
container-component hierarchy
with layout managers to organize
the way individual components
ABOUT US
Sample Swing Application
To demonstrate the power of
JavaFX, we’ll use a relatively
simple Swing application that has
enough complexity in its components but can be easily understood and modified. The first ap-
blog
PHOTOGRAPH BY BOB ADLER
Jasper Potts, the developer experience architect for
Java client, talks about the JavaFX 2.0 release.