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()
      • isResizable

        public boolean isResizable​()
        Description copied from class: Node
        Indicates whether this node is a type which can be resized by its parent. If this method returns true, then the parent will resize the node (ideally within its size range) by calling node.resize(width,height) during the layout pass. All Regions, Controls, and WebView are resizable classes which depend on their parents resizing them during layout once all sizing and CSS styling information has been applied.

        If this method returns false, then the parent cannot resize it during layout (resize() is a no-op) and it should return its layoutBounds for minimum, preferred, and maximum sizes. Group, Text, and all Shapes are not resizable and hence depend on the application to establish their sizing by setting appropriate properties (e.g. width/height for Rectangle, text on Text, and so on). Non-resizable nodes may still be relocated during layout.

        Overrides:
        isResizable in class Node
        Returns:
        whether or not this node type can be resized by its parent during layout
        See Also:
        Node.getContentBias(), Node.minWidth(double), Node.minHeight(double), Node.prefWidth(double), Node.prefHeight(double), Node.maxWidth(double), Node.maxHeight(double), Node.resize(double, double), Node.getLayoutBounds()
      • 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()