Creates a JComponent which contains the input JavaFX node. - Java JavaFX

Java examples for JavaFX:Node

Description

Creates a JComponent which contains the input JavaFX node.

Demo Code


    //package com.java2s;

    import java.awt.Dimension;

    import javafx.application.Platform;
    import javafx.embed.swing.JFXPanel;
    import javafx.geometry.Bounds;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javax.swing.JComponent;

    public class Main {
        /**//from www.  jav  a 2 s.  c  om
         * Creates a JComponent which contains the input JavaFX node.  The returned
         * JComponent will have a transparent background, allowing the JavaFX node
         * to control what is transparent and what is not.
         * @param pFxComponent
         * @return
         */
        public static JComponent createSwingPanelWithFxComponent(final Node pFxComponent)
{
   final JFXPanel fxpanel = new JFXPanel();
   Platform.runLater(() -> {
      StackPane pane = new StackPane(pFxComponent);
      Scene scene = new Scene(pane);
      scene.setFill(Color.TRANSPARENT);
      fxpanel.setScene(scene);
      Dimension d = convertToDimension(pFxComponent.getLayoutBounds());
      fxpanel.setPreferredSize(d);
      fxpanel.setMinimumSize(d);
   });
      
   return fxpanel;
}

        /**
         * Converts a Bound to a Dimension.  Note that some information will be
         * lost (depth, x, y, etc) yet the width and height will be preserved
         * @param pBound
         * @return
         */
        public static Dimension convertToDimension(Bounds pBound) {
            Dimension result = new Dimension();
            result.setSize(pBound.getWidth(), pBound.getHeight());
            return result;
        }
    }

Related Tutorials