等待函数返回,直到 JFrame 在 JavaFX 线程中关闭



下面的函数将Java FX面板放在Swing JDialog中,向JFX面板添加Web视图,并在其中呈现HTML报告。

我试图完成的是使displayHTMLOutput函数仅在显示HTML并且JDialog关闭时才返回。由于内容在 JavaFX 线程中呈现,因此我无法捕获关闭对话框。如何做到这一点?

/* Displays a given OutputTable object in a dialog */
public static void displayHTMLOutput(OutputTable table) {
    /* Create modal dialog and JFXPanel */
    JDialog dialog = new JDialog((Window) null);
    JFXPanel jfxPanel = new JFXPanel();
    /* Add JFXPanel to dialog */
    dialog.add(jfxPanel);
    /* Configure the dialog */
    dialog.setSize(1200, 800);
    dialog.setLocationRelativeTo(null);
    /* Maps escape key to closing the dialog */
    key(dialog);
    /* sets an icon for the dialog */
    ImageIcon img = new ImageIcon("icons/report.png");
    dialog.setIconImage(img.getImage());
    /* Show the dialog */
    dialog.setVisible(true);
    /* Runs ons the JavaFX Application Thread at a later time */
    Platform.runLater(() -> {
        /* Create webview and bind to scene */
        WebView webView = new WebView();        
        jfxPanel.setScene(new Scene(webView));
        /* Load the html in a webview */
        webView.getEngine().loadContent(DisplayHtml.getOutputHTMLOutputTable("template.html", table));      
        /* Do some actions on the dialog */
        dialog.setAlwaysOnTop(true);
        dialog.setModal(true);
        dialog.requestFocus();
    });
}

感谢@Slaw的引用帖子,我能够重写我的代码以获得所需的结果。事实证明,只有在将 Web 视图代码放置在 Java FX 线程上之后,窗口才应该可见。

/* Displays a given OutputTable object in a dialog */
public static void displayHTMLOutput(OutputTable table) {
    /* Create modal dialog and JFXPanel */
    JDialog dialog = new JDialog((Window) null);
    JFXPanel jfxPanel = new JFXPanel();
    /* Add JFXPanel to dialog */
    dialog.add(jfxPanel);
    /* Configure the dialog */
    dialog.setModalityType(ModalityType.APPLICATION_MODAL);
    dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    dialog.setSize(1200, 800);
    /* Maps escape key to closing the dialog */
    key(dialog);
    /* sets an icon for the dialog */
    ImageIcon img = new ImageIcon("icons/report.png");
    dialog.setIconImage(img.getImage());
    /* Runs ons the JavaFX Application Thread at a later time */
    Platform.runLater(() -> {
        /* Create webview and bind to scene */
        WebView webView = new WebView();        
        jfxPanel.setScene(new Scene(webView));
        /* Load the html in a webview */
        webView.getEngine().loadContent(DisplayHtml.getOutputHTMLOutputTable("template.html", table));      
    });
    /* Do some actions on the dialog */
    dialog.setAlwaysOnTop(true);
    dialog.requestFocus();
    dialog.setLocationRelativeTo(null);
    dialog.setVisible(true);
    /* Test is now only printed to console when the dialog is closed */
    System.out.println("test");     
}

相关内容

最新更新