从以加载的 FXML 作为内容的对话框启动线程时出现“不在 FX 应用程序线程上”错误

bas*_*aad 4 java dialog javafx javafx-8

我目前有以下情况:

我创建了一个JavaFX应用程序,其中包含一个屏幕,我可以从中打开一个对话框(只需单击屏幕上的按钮)。然后,用户输入并单击“应用”按钮。然后,用户输入被发送到一个方法,该方法打开某种进度对话框(用于向用户显示同步的状态,这对于问题来说并不重要)。我将此对话框称为“MyDialog”。MyDialog 使用以下代码构建:

Dialog<Void> dialog = new Dialog<>();
dialog.initOwner(null);
dialog.initStyle(StageStyle.UNDECORATED);
dialog.setHeaderText("Nieuw product synchroniseren...");
dialog.setResizable(false);

//Load dialog FXML file into the Pane
FXMLLoader fxmlloader = new FXMLLoader();
fxmlloader.setLocation(getClass().getResource("dialogs/MyDialogContent.fxml"));
try {
    dialog.getDialogPane().setContent(fxmlloader.load());
} catch (IOException e) {
    Functions.createExceptionDialog(e);
}
MyDialogContentController childController = fxmlloader.getController();

final ButtonType canceledButtonType = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);
dialog.getDialogPane().getButtonTypes().add(canceledButtonType);
Run Code Online (Sandbox Code Playgroud)

这很好用。MyDialog 中显示的进度条指示任务的进度。该任务是从一个线程开始的。该线程是从上屏幕的控制器启动的。在任务中,我想在某个时候显示一个附加对话框,以获得一些用户验证。我将此对话框称为“AlertDialog”。这是该部分的代码(放置在上屏幕的控制器中,而不是 MyDialog 的控制器中):

Task<Object> task = new Task<Object>() {

    @Override
    protected Object call() {
        //Show choice dialog
        Alert alert = new Alert(AlertType.CONFIRMATION);
        alert.initOwner(null);
        alert.initStyle(StageStyle.UNDECORATED);

        ButtonType buttonTypeOne = new ButtonType("One");
        ButtonType buttonTypeTwo = new ButtonType("Two");
        ButtonType buttonTypeThree = new ButtonType("Three");
        ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);

        alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree, buttonTypeCancel);

        Optional<ButtonType> result = alert.showAndWait();

        if (result.get() == buttonTypeOne){
            //User chose "One";
        } else if (result.get() == buttonTypeTwo) {
            // ... user chose "Two"
        } else if (result.get() == buttonTypeThree) {
            // ... user chose "Three"
        } else {
            // ... user chose CANCEL or closed the dialog
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,AlertDialog 没有显示,并且我收到以下错误:

Exception in thread "Thread-10" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-10
Run Code Online (Sandbox Code Playgroud)

我已经尝试过以下解决方案:

  • 将 AlertDialog 代码放置在 MyDialogController 中,然后从任务中调用它。
  • 从 MyDialogController 启动线程,而不是从上部屏幕控制器启动。然后通过“childController.thread”调用它。

这两种解决方案均无效。我希望它与在 DialogPane 中加载 FXML 文件有关,因此与线程及其启动位置有关。

那么这种情况下的问题是:

  1. 为什么我会收到此错误?

  2. 该错误是否与 AlertDialog 未显示有关?

  3. 我对这部分代码的处理方法应该有所不同吗?(例如,不在对话框中加载外部 FXML 文件)

任何帮助是极大的赞赏!

eck*_*kig 6

我觉得我已经在 SO 上写了 100 遍了:JavaFX 是一个单线程 GUI,因此与 GUI 相关的所有事情都必须在主 JavaFX 线程上完成。

如果您尝试在 JavaFX 线程之外执行一些与 GUI 相关的操作,您将得到IllegalStateException: Not on FX application thread.

Optional<ButtonType> result = alert.showAndWait();是一个 GUI 操作,因为您初始化并显示Dialog. 因此,您应该在其他地方检索用户输入,并且只在后台进行长时间运行的计算。

用户输入的优点是例如Task类的各种生命周期挂钩(如succeeded()failed())。