Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed JPanel into JavaFX pane?

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

like image 665
jordan Avatar asked Mar 26 '15 04:03

jordan


1 Answers

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);
     }
 }
like image 193
ItachiUchiha Avatar answered Nov 09 '22 04:11

ItachiUchiha