I have a huge swing application and I wanted to embed javafx into it. I tried many times to do that (by following oracle tutorials etc) and I only succeed when I declared a new JFrame to use the JFXPanel component. But, I don't want to use a new Frame, I want to incorporate my Javafx code into the root JFrame of the swing application.
Can we embed javaFX components into JPanel instead of JFrame ? If the answer is yes, why didn't I succeed ?
There is the code sample that is probably wrong :
This class directly extends JPanel and the initialize method is called in an EDT
private void initialize(){
setLayout(new BorderLayout());
final JFXPanel fxPanel = new JFXPanel();
JPanel jp = new JPanel();
jp.add(fxPanel);
jp.setVisible(true);
jp.setSize(500, 300);
jp.setBackground(Color.CYAN);
Platform.runLater(new Runnable() {
@Override
public void run() {
initFX(fxPanel);
}
});
add(createButtonsPanel(), BorderLayout.NORTH);
add(jp,BorderLayout.CENTER);
}
private static void initFX(JFXPanel fxPanel) {
Scene scene = initScene();
fxPanel.setScene(scene);
}
private static Scene initScene(){
Group root = new Group();
Scene scene = new Scene(root, javafx.scene.paint.Color.ALICEBLUE );
Text text = new Text();
text.setX(40);
text.setY(100);
text.setFont(new Font(25));
text.setText("Welcome JavaFX!");
root.getChildren().add(text);
return (scene);
}