How to embed JPanel into JavaFX pane?
13,042
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);
}
}
Author by
jordan
Updated on June 11, 2022Comments
-
jordan almost 2 years
How can I add a
swingNode
to aspecific
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.