In this tutorial, we will learn, how to embed Swing components in JavaFX applications.
We have to use the ‘SwingNode’ class to use swing components in JavaFX.
First, create an instance of SwingNode class.
SwingNode swingNode = new SwingNode();
then call the ‘setContent()’ method on either the JavaFX application thread or event dispatch thread (EDT).
However, to access the Swing content, ensure that your code runs on EDT because the standard Swing threading restrictions apply.
//calling setContent() method on javafx application thread
private void createSwingContent(final SwingNode swingNode) {
private void createSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
swingNode.setContent(new JButton(“Click me!”)); }
});
}
For better clarification and understanding of the whole concept please check this video tutorial.