9

How can I add a swingNode to a specific pane?

I'm actually trying to add a JPanel that loads an applet to the transparent area of the following and I'm not sure how to do it.

enter image description here

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
jordan
  • 187
  • 1
  • 2
  • 9

1 Answers1

20

SwingNode is a javafx scene node and can be added to any javafx scene layouts.

To add a JPanel to a Pane and display it on JavaFX stage:

  • Add JPanel to a SwingNode
  • Assign the swingnode as a child to any of the layouts (which includes Pane).
  • Set the layout as the root of the scene
  • Set the scene to the stage and display it

A very simple code sample to show how you can add it to a Pane is (from SwingNode Javadoc):

public class SwingNodeExample extends Application {

     @Override
     public void start(Stage stage) {
         final SwingNode swingNode = new SwingNode();
         createAndSetSwingContent(swingNode);

         Pane pane = new Pane();
         pane.getChildren().add(swingNode); // Adding swing node

         stage.setScene(new Scene(pane, 100, 50));
         stage.show();
     }

     private void createAndSetSwingContent(final SwingNode swingNode) {
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                 JPanel panel = new JPanel();
                 panel.add(new JButton("Click me!"));
                 swingNode.setContent(panel);
             }
         });
     }

     public static void main(String[] args) {
         launch(args);
     }
 }
MikaelF
  • 3,518
  • 4
  • 20
  • 33
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176