Home » Java » Desktop Java » Integrating JavaFX 2.0 with Swing and SWT

About Dustin Marx

Dustin Marx

Integrating JavaFX 2.0 with Swing and SWT

One of the improvements in JavaFX with JavaFX 2.0 has been greater ease of interoperability with Swing and SWT. Several online resources document how this is done. These include Integrating JavaFX into Swing Applications and SWT Interop. However, in a nice example of effective class-level Javadoc documentation, the respective JavaFX classes javafx.embed.swing.JFXPanel and javafx.embed.swt.FXCanvas each provide a simple code sample of how to use the class to embed JavaFX into Swing or SWT code. In this post, I build upon the code samples provided in these classes’ Javadoc documentation to demonstrate JavaFX integration with Swing and SWT.

Both JFXPanel and FXCanvas allow a JavaFX Scene to be set on their instance. The instance of Scene (based on my Simple JavaFX 2.0 Text Example post) to be be used in my examples in this post are provided by the method shown in the next JavaFX-specific code example.

Method Providing a JavaFX Scene for Integration

package dustin.examples;

import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

 * Simple class intended to be used by two examples of integrating JavaFX with
 * Swing and with SWT. Provides single method {@code createScene()} to be used
 * by the classes that are examples of integrating Swing with JavaFX and SWT
 * with JavaFX.
 * @author Dustin
public class TextIntegrationSceneCreator
    * Provides an instance of Scene with JavaFX text examples.
    * @return Instance of Scene with text examples.
   public static Scene createTextScene()
      final Group rootGroup = new Group();
      final Scene scene = new Scene(rootGroup, 800, 400, Color.BEIGE);

      final Text text1 = new Text(25, 25, "(2007) JavaFX based on F3");
      text1.setFont(Font.font(java.awt.Font.SERIF, 25));

      final Text text2 = new Text(25, 50, "(2010) JavaFX Script Deprecated");
      text2.setFont(Font.font(java.awt.Font.SANS_SERIF, 30));

      final Text text3 = new Text(25, 75, "(2011) JavaFX to be Open Sourced!");
      text3.setFont(Font.font(java.awt.Font.MONOSPACED, 35));

      final Text text4 = new Text(25, 125, "(2011) JavaFX to be Standardized");
      text4.setFont(Font.font(java.awt.Font.DIALOG, 40));
      final Effect glow = new Glow(1.0);

      final Text text5 = new Text(25, 175, "(Now) Time for JavaFX 2.0!");
      text5.setFont(Font.font(java.awt.Font.SERIF, FontWeight.EXTRA_BOLD, 45));
      final Light.Distant light = new Light.Distant();
      final Lighting lighting = new Lighting();

      final Text text6 = new Text(25, 225, "JavaFX News at JavaOne!");
      text6.setFont(Font.font(java.awt.Font.DIALOG_INPUT, FontWeight.THIN, 45));
      final Reflection reflection = new Reflection();

      return scene;      

A JavaFX Scene can be integrated into Swing code via the JavaFX class JFXPanel and its setScene(Scene) method. This is demonstrated in the next code listing, which gets the particular Scene instance from the method in the previous code listing.

JavaFX/Swing Integration with JFXPanel

package dustin.examples;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

 * Simple class demonstrating interoperability between Swing and JavaFX. This
 * class is adapted from the example provided in the Javadoc documentation for
 * {@code javafx.embed.swing.JFXPanel}.
public class SwingJavaFxInteroperabilityDemo
   private static void initAndShowGUI()
      // This method is invoked on Swing thread
      final JFrame frame = new JFrame("JavaFX / Swing Integrated");
      final JFXPanel fxPanel = new JFXPanel();

      Platform.runLater(new Runnable()
         public void run()

   private static void initFX(JFXPanel fxPanel)
      // This method is invoked on JavaFX thread
      final Scene scene = TextIntegrationSceneCreator.createTextScene();

   public static void main(String[] arguments)
      SwingUtilities.invokeLater(new Runnable()
         public void run()

The output of running this simple Java Swing application with embedded JavaFX Scene is shown next.

Integrating SWT with JavaFX is arguably even easier and is demonstrated in the next code listing. As with the Swing integration example, the main approach is to call FXCanvas‘s setScene(Scene) method.

JavaFX/SWT Integration with FXCanvas

package dustin.examples;

import javafx.embed.swt.FXCanvas;
import javafx.scene.Scene;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

 * Simple class demonstrating interoperability between SWT and JavaFX. This
 * class is based on the example provided in the Javadoc documentation for
 * {@code javafx.embed.swt.FXCanvas}.
 * @author Dustin
public class SwtJavaFxInteroperabilityDemo
   public static void main(String[] arguments)
      final Display display = new Display();
      final Shell shell = new Shell(display);
      shell.setText("JavaFX / SWT Integration");
      shell.setLayout(new FillLayout());
      final FXCanvas canvas = new FXCanvas(shell, SWT.NONE);
      final Scene scene = TextIntegrationSceneCreator.createTextScene();
      while (!shell.isDisposed())
         if (!display.readAndDispatch()) display.sleep();

The next screen snapshot shows what this simple SWT application with embedded JavaFX looks like.

The code listings for Swing integration with JavaFX and for SWT integration with JavaFX shown above are only slightly adapted from the Javadoc documentation for the JavaFX classes JFXPanel (Swing) and FXCanvas (SWT). It is nice that these classes provide these examples in their documentation and it is really nice that integration has become so much easier. For more thorough coverage of JavaFX/Swing integration, see Integrating JavaFX into Swing Applications.

Reference: Integrating JavaFX 2.0 with Swing and SWT   from our JCG partner Dustin Marx  at the Inspired by Actual Events blog

Related Articles :

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments