Module javafx.swing

Class SwingNode

  • All Implemented Interfaces:
    Styleable, EventTarget

    public class SwingNode
    extends Node
    This class is used to embed a Swing content into a JavaFX application. The content to be displayed is specified with the setContent(javax.swing.JComponent) method that accepts an instance of Swing JComponent. The hierarchy of components contained in the JComponent instance should not contain any heavyweight components, otherwise SwingNode may fail to paint it. The content gets repainted automatically. All the input and focus events are forwarded to the JComponent instance transparently to the developer.

    Here is a typical pattern which demonstrates how SwingNode can be used:

         public class SwingFx extends Application {
    
             @Override
             public void start(Stage stage) {
                 final SwingNode swingNode = new SwingNode();
                 createAndSetSwingContent(swingNode);
    
                 StackPane pane = new StackPane();
                 pane.getChildren().add(swingNode);
    
                 stage.setScene(new Scene(pane, 100, 50));
                 stage.show();
             }
    
             private void createAndSetSwingContent(final SwingNode swingNode) {
                 SwingUtilities.invokeLater(new Runnable() {
                     @Override
                     public void run() {
                         swingNode.setContent(new JButton("Click me!"));
                     }
                 });
             }
    
             public static void main(String[] args) {
                 launch(args);
             }
         }
     
    Since:
    JavaFX 8.0
    • Constructor Detail

      • SwingNode

        public SwingNode()
        Constructs a new instance of SwingNode.
    • Method Detail

      • setContent

        public void setContent​(JComponent content)
        Attaches a JComponent instance to display in this SwingNode.

        The method can be called either on the JavaFX Application thread or the Event Dispatch thread. Note however, that access to a Swing component must occur from the Event Dispatch thread according to the Swing threading restrictions.

        Parameters:
        content - a Swing component to display in this SwingNode
        See Also:
        EventQueue.isDispatchThread(), Platform.isFxApplicationThread()
      • getContent

        public JComponent getContent()
        Returns the JComponent instance attached to this SwingNode.

        The method can be called either on the JavaFX Application thread or the Event Dispatch thread. Note however, that access to a Swing component must occur from the Event Dispatch thread according to the Swing threading restrictions.

        Returns:
        the Swing component attached to this SwingNode
        See Also:
        EventQueue.isDispatchThread(), Platform.isFxApplicationThread()
      • prefWidth

        public double prefWidth​(double height)
        Returns the SwingNode's preferred width for use in layout calculations. This value corresponds to the preferred width of the Swing component.
        Overrides:
        prefWidth in class Node
        Parameters:
        height - the height that should be used if preferred width depends on it
        Returns:
        the preferred width that the node should be resized to during layout
        See Also:
        Node.isResizable(), Node.getContentBias(), Node.autosize()
      • prefHeight

        public double prefHeight​(double width)
        Returns the SwingNode's preferred height for use in layout calculations. This value corresponds to the preferred height of the Swing component.
        Overrides:
        prefHeight in class Node
        Parameters:
        width - the width that should be used if preferred height depends on it
        Returns:
        the preferred height that the node should be resized to during layout
        See Also:
        Node.getContentBias(), Node.autosize()
      • maxWidth

        public double maxWidth​(double height)
        Returns the SwingNode's maximum width for use in layout calculations. This value corresponds to the maximum width of the Swing component.
        Overrides:
        maxWidth in class Node
        Parameters:
        height - the height that should be used if maximum width depends on it
        Returns:
        the maximum width that the node should be resized to during layout
        See Also:
        Node.isResizable(), Node.getContentBias()
      • maxHeight

        public double maxHeight​(double width)
        Returns the SwingNode's maximum height for use in layout calculations. This value corresponds to the maximum height of the Swing component.
        Overrides:
        maxHeight in class Node
        Parameters:
        width - the width that should be used if maximum height depends on it
        Returns:
        the maximum height that the node should be resized to during layout
        See Also:
        Node.isResizable(), Node.getContentBias()
      • minWidth

        public double minWidth​(double height)
        Returns the SwingNode's minimum width for use in layout calculations. This value corresponds to the minimum width of the Swing component.
        Overrides:
        minWidth in class Node
        Parameters:
        height - the height that should be used if minimum width depends on it
        Returns:
        the minimum width that the node should be resized to during layout
        See Also:
        Node.isResizable(), Node.getContentBias()
      • minHeight

        public double minHeight​(double width)
        Returns the SwingNode's minimum height for use in layout calculations. This value corresponds to the minimum height of the Swing component.
        Overrides:
        minHeight in class Node
        Parameters:
        width - the width that should be used if minimum height depends on it
        Returns:
        the minimum height that the node should be resized to during layout
        See Also:
        Node.isResizable(), Node.getContentBias()