DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Integrating JavaFX 2.0 with Swing and SWT

Integrating JavaFX 2.0 with Swing and SWT

Dustin Marx user avatar by
Dustin Marx
·
Jan. 14, 12 · Interview
Like (0)
Save
Tweet
Share
19.24K Views

Join the DZone community and get the full member experience.

Join For Free

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.setFill(Color.CHOCOLATE);
      text1.setFont(Font.font(java.awt.Font.SERIF, 25));
      rootGroup.getChildren().add(text1);

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

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

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

      final Text text5 = new Text(25, 175, "(Now) Time for JavaFX 2.0!");
      text5.setFill(Color.DARKVIOLET);
      text5.setFont(Font.font(java.awt.Font.SERIF, FontWeight.EXTRA_BOLD, 45));
      final Light.Distant light = new Light.Distant();
      light.setAzimuth(-135.0);
      final Lighting lighting = new Lighting();
      lighting.setLight(light);
      lighting.setSurfaceScale(9.0);
      text5.setEffect(lighting);
      rootGroup.getChildren().add(text5);

      final Text text6 = new Text(25, 225, "JavaFX News at JavaOne!");
      text6.setFill(Color.DARKGREEN);
      text6.setBlendMode(BlendMode.COLOR_BURN);
      text6.setFont(Font.font(java.awt.Font.DIALOG_INPUT, FontWeight.THIN, 45));
      final Reflection reflection = new Reflection();
      reflection.setFraction(1.0);
      text6.setEffect(reflection);
      rootGroup.getChildren().add(text6);

      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();
      frame.add(fxPanel);
      frame.setVisible(true);

      Platform.runLater(new Runnable()
      {
         @Override
         public void run()
         {
            initFX(fxPanel);
         }
      });
   }

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

   public static void main(String[] arguments)
   {
      SwingUtilities.invokeLater(new Runnable()
      {
         @Override
         public void run()
         {
            initAndShowGUI();
         }
      });
   }   
}

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();
      canvas.setScene(scene);
      shell.open();
      while (!shell.isDisposed())
      {
         if (!display.readAndDispatch()) display.sleep();
      }
      display.dispose();
   }   
}

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.

 

From http://marxsoftware.blogspot.com/2011/12/integrating-javafx-20-with-swing-and.html

Standard Widget Toolkit JavaFX

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • HTTP vs Messaging for Microservices Communications
  • Custom Validators in Quarkus
  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?
  • What Is API-First?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: