Embedding JavaFX in Swing

时间秒杀一切 提交于 2021-01-29 11:09:04

问题


This is my first question here so I'm a bit nervous, but let's get straight to the point. I'm trying to embed a JavaFX scene in a JFrame and I don't seem to quite succeed. The scene sometimes renders properly, but other times it's just grey background. I've been trying to think of a solution for the last couple days, but I just can't seem to find one. Here's the code:

public class Popup {
public Popup() {
    SwingUtilities.invokeLater(() -> {
        JFrame frame = new JFrame("popup");
        frame.setUndecorated(true);
        frame.setType(Window.Type.POPUP);
        JFXPanel panel = new JFXPanel();
        frame.add(panel);
        Platform.runLater(() -> {
            VBox root = new VBox();
            root.setStyle("-fx-background: red;");
            Scene scene = new Scene(root);
            Label label = new Label("hello friend");
            Label other = new Label("hello WORLD!!!");
            root.getChildren().addAll(label, other);
            panel.setScene(scene);
            /*synchronized (this) {
                try {
                    wait(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }*/

            SwingUtilities.invokeLater(() -> {
                frame.pack();
                frame.setVisible(true);
            });
        });
    });
}

I'm not sure why, but while uncommenting the piece of code that I commented out seems to make it much more likely for the scene to render properly. I apologies for my bad English, it's not my native language.


回答1:


Here is the example which has JavaFX components in a Swing JFrame:

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javax.swing.*;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import java.awt.Point;
public class FxInSwing {
    private JFrame frame;
    public static void main(String... args) {
        SwingUtilities.invokeLater(() -> {
            new FxInSwing().initAndShowGUI();
        });
    }
    /*
     * Builds and displays the JFrame with the JFXPanel.
     * This method is invoked on the Swing Event Dispatch Thread -
     * see the main().
     */
    private void initAndShowGUI() {
        frame = new JFrame("JavaFX in Swing App");
        JFXPanel fxPanel = new JFXPanel();
        frame.add(fxPanel);
        frame.setSize(400, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocation(new Point(150, 150));
        Platform.runLater(() -> {
            fxPanel.setScene(createScene());
        });
    }
    private Scene createScene() {
        VBox root = new VBox();
        root.setStyle("-fx-background: red;");
        Label label = new Label("hello friend");
        Label other = new Label("hello WORLD!!!");
        root.getChildren().addAll(label, other);
        return new Scene(root);
    }
}


来源:https://stackoverflow.com/questions/51870093/embedding-javafx-in-swing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!