Java Reference
In-Depth Information
}
The resulting application looks like Figure 2-2 .
Figure 2-2 . Using SwingNode to embed a JLabel
How It Works
Swing was once the preferred means for developing graphical user interfaces and
desktop applications for the Java platform. Swing is part of the Java Foundation
Classes (JFC), a grouping of features for developing graphical user interfaces. The
Swing API consists of a multitude of components ( JComponent classes), event hand-
ling classes, look and feel classes, and so forth. The API is quite capable of developing
highly sophisticated applications. However, the API is so immense that it can be diffi-
cult to learn, and technology has evolved in a way such that many of the Swing classes
and features have become outdated. That being said, JavaFX has become the preferred
API for developing modern graphical user interfaces and desktop applications for the
Java platform. As such, many developers need a way to meld the two APIs together in
order to transition. Even applications written entirely in JavaFX can benefit from the
use of some Swing components. The javafx.embed.swing.SwingNode class
makes it possible to embed a JComponent instance into a JavaFX application with
little effort, by passing the JComponent to the SwingNode setContent()
method. The content is repainted automatically and all events are forwarded to the
JComponent instance without user intervention.
In the example to this recipe, a Swing JLabel component is embedded into a
JavaFX application by passing the Swing component to the SwingNode setCon-
tent() method. The Swing content should run on the Event Dispatch Thread (EDT),
 
 
Search WWH ::




Custom Search