Class JFXPanel

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable

public class JFXPanel extends JComponent
JFXPanel is a component to embed JavaFX content into Swing applications. The content to be displayed is specified with the setScene(javafx.scene.Scene) method that accepts an instance of JavaFX Scene. After the scene is assigned, it gets repainted automatically. All the input and focus events are forwarded to the scene transparently to the developer.

There are some restrictions related to JFXPanel. As a Swing component, it should only be accessed from the event dispatch thread, except the setScene(javafx.scene.Scene) method, which can be called either on the event dispatch thread or on the JavaFX application thread.

Here is a typical pattern how JFXPanel can used:

     public class Test {

         private static void initAndShowGUI() {
             // This method is invoked on Swing thread
             JFrame frame = new JFrame("FX");
             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
             Scene scene = createScene();
             fxPanel.setScene(scene);
         }

         public static void main(String[] args) {
             SwingUtilities.invokeLater(new Runnable() {
                 @Override
                 public void run() {
                     initAndShowGUI();
                 }
             });
         }
     }
 
Warning: Serialized objects of this class will not be compatible with future Swing or JavaFX releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing and the same version of JavaFX.
Since:
JavaFX 2.0
See Also:
  • Constructor Details

    • JFXPanel

      public JFXPanel()
      Creates a new JFXPanel object.

      Implementation note: when the first JFXPanel object is created, it implicitly initializes the JavaFX runtime. This is the preferred way to initialize JavaFX in Swing.

  • Method Details