Module javafx.swing

Class SwingNode

java.lang.Object
javafx.scene.Node
javafx.embed.swing.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 Details

    • SwingNode

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

    • 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:
    • 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:
    • resize

      public void resize(double width, double height)
      Invoked by the SwingNode's parent during layout to set the SwingNode's width and height. Applications should not invoke this method directly. If an application needs to directly set the size of the SwingNode, it should set the Swing component's minimum/preferred/maximum size constraints which will be propagated correspondingly to the SwingNode and it's parent will honor those settings during layout.
      Overrides:
      resize in class Node
      Parameters:
      width - the target layout bounds width
      height - the target layout bounds height
      See Also:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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: