create a JFXPanel that wraps the JavaFX
nodes and can be manipulated in a
Swing GUI in the same way as any other
Component object.
We construct a new, final instance of
JFXPanel and then set up a task to run
on the JavaFX thread to construct the
toolbar node. It is very important that
all manipulation of the JavaFX scene be
handled in this way.
Within the run method, we create
each element of the toolbar as a Button
control. To match the original format, we
use only a graphical icon (implemented
with ActiveIcon) with no text. Adding
tooltip text is a simple method call.
To respond to the user clicking a
button, we add a new event handler
to each button. Again, within the handler method it is important that all
activities happen on the Swing event
thread, so we add a Runnable object via
the SwingUtilities invokeLater method.
All the run method has to do is invoke
the appropriate actionPerformed
method that would be called from the
Swing toolbar via registered listeners.
Strictly speaking, we should pass valid
ActionEvent objects to these methods,
but none of the methods use the argument, so for brevity of code we pass null.
The toolbar itself is constructed by
passing references to the buttons we’ve
created, along with any required separators, to the constructor. The items in a
toolbar are typically Button, ToggleButton,
and Separator objects, but any Node
can be used. So, it would be simple to
replace a static separator with a more
dynamic, custom version.
Replacing the Old Toolbar
The last piece is to replace the instance
of the JToolbar with our new JavaFX version. Listing 5 shows the changes to the
initMainGUI method.
In order for the preferences to
work (and to avoid a null-pointer exception), we must also change the
synchGui WithGeneralLookPrefs method,
as shown in Listing 6.
public class ActiveIcon extends Parent {
public static final int TYPE_NONE = 0;
public static final int TYPE_FADE = 1;
public static final int TYPE_SPIN = 2;
public static final int TYPE_GROW = 3;
public static final int TYPE_SLIDE = 4;
public ActiveIcon(String fileName, int type) {
URL imageURL = getClass().getResource(fileName);
JAVA IN ACTION
Conclusion
In this series of articles, we’ve looked at
how to integrate JavaFX scene graphs
into a Swing application by wrapping the
scene graph as a Component, which can
easily be manipulated by a layout manager. We’ve also looked at how events
can be handled using both the Swing
event dispatch thread and the JavaFX
application thread. The real benefit of
having this kind of integration is that
you can gradually migrate an existing
Swing application to JavaFX without
having to rewrite it all in one go. As the
examples have shown, it is straightforward to replace tables, toolbars, and
other standard components and have
these new components add rich, visually
appealing effects such as translucency
and animations.
JavaFX is an incredibly rich and powerful way to develop exciting new user
interfaces in Java. Get started! </article>
if (imageURL == null) {
System.out.println("NULL image URL");
return;
}
Image iconImage = new Image( imageURL.toString());
ImageView iconView = new ImageView(iconImage);
double width = iconImage.get Width();
…
getChildren().add(iconView);
ABOUT US
LEARN MORE
•;”JavaFX and Swing Integration, Part 1”
•;”JavaFX and Swing Integration, Part 2”
•;Documentation and tutorials
blog