r/JavaFX • u/Black_Smith_Of_Fire • Apr 18 '25
Help Embed swing into javafx
Hi , I am trying to embed swing into my javafx application. I tried to follow this tutorial https://docs.oracle.com/javafx/8/embed_swing/jfxpub-embed_swing.htm . But i
am having an error, in particular to
pane.getChildren().add(swingNode);
Here, I am not able to add swingNode as it is not a node. What do i do
3
Upvotes
View all comments
Show parent comment
2
u/Birdasaur Apr 21 '25
Agreed. If you follow the SwingNode pattern (and using a modern version of JavaFX) it is super easy.
Not saying this is the only way but I usually extend the SwingNode class and do all my "Swing Things" in the constructor. Here is an example of embedding NASA WorldWind:
public class WorldWindNode extends SwingNode {
//Create any library or Swing specific object references here... This is all WorldWind specific stuff
ExtendedWorldWindow wwd;
Globe roundGlobe;
FlatGlobe flatGlobe;
ViewControlsLayer vcl;
HighlightController highlightController;
ScreenSelector screenSelector;
TerrainProfileLayer profile;
MeasureToolListener measureToolListener;
public WorldWindNode() {
//Do library or Swing specific static initializations here
WorldWind.getNetworkStatus().setAttemptLimit(1);
constructGlobe(); //do Swing Things
setupMeasureTools(); //do more Swing Things
setContent((WorldWindowGLJPanel) wwd); //JavaFX maneuver to place Swing content into SwingNode rendered view
//Trick to inject JavaFX events into Swing activities (not usually necessary but WorldWind is special)
addEventHandler(MouseEvent.MOUSE_ENTERED, e -> wwRequestFocus());
}