如何避免"Not on FX application thread; currentThread = JavaFX Application Thread"错误?

66

下面的代码片段给我报错:Not on FX application thread; currentThread=JavaFX Application Thread。这个应用在Java 1.7中正常工作,但当我将它移植到FX8时就出现了错误。第一次启动应用程序时,它按预期工作。但是关闭舞台并再次打开后,它就无法工作了。

错误也不太清楚:Not On fx application thread and current thread- javafx application thread。如果当前线程是fx应用程序线程,那么“不在fx应用程序线程上”是什么意思呢?

progressDialog = createProgressDialog(service);
progressDialog.show();
progressDialog.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        // if (service.isRunning()) {
        // service.cancel();
        progressDialog.close();
        // }
    }
});
@SuppressWarnings("unchecked")
private Stage createProgressDialog(final Service<IStatus> service) {
    stage = new Stage();

    URL url = FileLocator.find(Activator.getDefault().getBundle(),
    new Path("icons/xxx_16x16.png"), null); //$NON-NLS-1$
    stage.getIcons().add(new Image(url.getFile()));
    stage.setTitle("Downloading ..."); //$NON-NLS-1$
    // Creating StackPane
    stage.initModality(Modality.WINDOW_MODAL);
}

编辑你的代码,使其成为一个 sscce,答案就会变得显而易见。 - jewelsea
7个回答

101

呼叫

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        // do your GUI stuff here
    }
});

会修复它。


6
如果你正在开发纯JavaFX应用程序,那么这将有效。但是我使用的是SWT,在FXCanvas上创建FX组件。如果我在Platform.runLater(new Runnable(){ // ... });中调用UI创建,则会抛出“工具包未初始化”的错误。 - Rajesh Kumar Dash

44

当我在JavaFX 2中修改类似于listview元素的UI元素时,发生了这种情况。修改场景图的任务帮助我解决了问题,即通过

 final ListView<String> group = new ListView ();

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

     @Override protected Void call() throws Exception {

         group.getItems().clear();  

          for (int i=0; i<100; i++) {                
             Platform.runLater(new Runnable() {
                 @Override public void run() {
                     group.getItems.add(i);
                 }
             });
         }
         return null;
     }
 };

1
你的解决方案也解决了我的问题。我仍然不明白为什么。有人能解释一下为什么“Platform.runLater(new Runnable(){});”可以解决它吗? - Yannic Hansen
2
@Yannic Hansen,这是因为在JavaFX中只有FX线程才能修改UI元素。这就是为什么要使用Platform.runLater的原因。 - GOXR3PLUS
这也解决了我的问题,我一直在尝试每1分钟更新窗口标题。立即生效了。 - Wesos de Queso

23

当您尝试更改某个组件的UI(例如标签文本)时,应该会发生这种情况。始终以这种方式运行:

@FXML Label myLabel;

Platform.runLater(new Runnable(){
   myLabel.setText("some text");
});

19

Platform.setImplicitExit(false);解决了我的问题。我认为在JavaFX 8中更改了实现方式,因此在JavaFX 2中没有任何问题的相同代码会出现“not an fx application thread”错误。


7

您可以在代码的任何部分使用此功能更改表单或转到另一个视图或fxml:

Platform.runLater(() -> {
                            try {
                                Stage st = new Stage();
                                Parent sceneMain = FXMLLoader.load(getClass().getResource("/com/load/free/form/LoadFile.fxml"));
                                Scene scene = new Scene(sceneMain);
                                st.setScene(scene);
                                st.setMaximized(true);
                                st.setTitle("load");
                                st.show();
                            } catch (IOException ex) {
                                Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        });

我的控制器中的示例:

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;


public class LoginController implements Initializable {

    @FXML
    private TextField txtUser;

    @FXML
    private TextField txtPassword;

    @FXML
    private Hyperlink urlForgetPassword;

    @FXML
    private Label lblError;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }

    public void isLoginAction(ActionEvent event) {
        String message = "Ingrese ";
        boolean isEmtpy = false;

        if (txtUser.getText().trim().isEmpty()) {
            message += "usuario y ";
            isEmtpy = true;
        }

        if (txtPassword.getText().trim().isEmpty()) {
            message += "contraseña  ";
            isEmtpy = true;
        }
        isEmtpy = false;
        if (isEmtpy) {
            message = message.substring(0, message.length() - 2);
            lblError.getStyleClass().remove("message_process");
            lblError.getStyleClass().add("message_error");
            lblError.setText(message);
        } else {
            lblError.getStyleClass().add("message_process");
            lblError.getStyleClass().remove("message_error");
            Task task = new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    updateMessage("Procesando...");
                    System.out.println("Asignando DATOS DE PRUEBA ");
                    String passEnc = Encripta.encriptar(txtPassword.getText(), Encripta.HASH_SHA1);
                    int typeRest = new RestConnection().getConnectionUser(txtUser.getText(), passEnc);
                    if (typeRest == 1) {
                        //Load Another form
                        Platform.runLater(() -> {
                            try {
                                Stage st = new Stage();
                                Parent sceneMain = FXMLLoader.load(getClass().getResource("/com/load/free/form/LoadFile.fxml"));
                                Scene scene = new Scene(sceneMain);
                                st.setScene(scene);
                                st.setMaximized(true);
                                st.setTitle("");
                                st.show();
                            } catch (IOException ex) {
                                Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        });

                    } else {
                        lblError.getStyleClass().remove("message_process");
                        lblError.getStyleClass().add("message_error");
                        updateMessage("Usuario y/o contraseña incorrectos");
                    }
                    return null;
                }
            };

            lblError.textProperty().bind(task.messageProperty());
            new Thread(task).start();

        }

    }

}

5
在上面的代码中没有明确显示,但我相当确定的是,在应用程序(主)JavaFX线程之外的某个地方,您正在创建一个线程,然后尝试在第二个线程上执行与JavaFX对象(如关闭、打开窗口等)相关的操作。这是严格禁止的,因为只有主线程可以直接控制JavaFX对象。如果这成为您的程序的要求,您需要使用第二个线程进行其他计算等操作,则必须使用某种形式的消息传递来让其他线程知道您想要执行任何JavaFX操作。

2
这个特别解决了我在这里遇到的问题,尝试执行 Thread initThread = new Thread(new Runnable() { ... scraper.init(); navButton.setText("Finished!"); ... }); initThread.start(); ,而我不知道其他线程尝试访问JavaFX应用程序线程是被禁止的(显然应该是这样的:P)。我将实现消息传递。谢谢。 - Inversus

1
我在为我的程序添加启动画面时遇到了同样的问题。这是我的代码:
CircleTest pForm = new CircleTest();

Task<Void> task = new Task<Void>() {
    @Override
    public Void call() {

        try {
            FXMLLoader loader = new FXMLLoader(
                Main.class.getResource("/newRegistration/HomePage.fxml"));
            AnchorPane page = (AnchorPane) loader
                .load();
            dialogStagee.getIcons().add(new Image("/piks/showthumb.png"));
            dialogStagee.setTitle("WALGOTECH SOLUTIONS: 0703445354");
            dialogStagee.initModality(Modality.APPLICATION_MODAL);
            Scene scene = new Scene(page);
            dialogStagee.setScene(scene);
            HomePageController controller = loader.getController();
            controller.setDialogStage(dialogStagee);
            dialogStagee.setMaximized(true);
            dialogStagee.initStyle(StageStyle.UNDECORATED);

            conn.prepareStatement(
                "INSERT INTO `logs`(`date`,`user`,`Terminal`,`Action`)"
                    + " VALUES ('"
                    + (java.time.LocalDate.now()
                    + " "
                    + java.time.LocalTime.now().getHour()
                    + ":"
                    + java.time.LocalTime.now().getMinute()
                    + ":" + java.time.LocalTime.now().getSecond())
                    + "',"
                    + "'"
                    + DbConnector.getMyVariablepatientvameloginCategory()
                                                        .split("as")[1]
                    + "',"
                    + "'"
                    + StartPageController.hostname
                    + "',"
                    + " 'Logged in into Registration')")
                                        .execute();
            } catch (Exception e) {
                    e.printStackTrace();
            }   

            return null;
        }
};
pForm.activateProgressBar(task);

task.setOnSucceeded(event -> {

    dialogStagee.show();

    try {

    } catch (Exception e) {

    }
    pForm.getDialogStage().close();
});

pForm.getDialogStage().show();
dialogStage.close();

Thread thread = new Thread(task);
thread.start();

运行代码时出现“未在fx应用程序上”的错误。通过在任务中添加Platform.runLater()解决了这个问题。 现在,这是我当前的代码:
CircleTest pForm = new CircleTest();

Task<Void> task = new Task<Void>() {
    @Override
    public Void call() {
        Platform.runLater(new Runnable() {

            @Override
            public void run() {

                try {
                    FXMLLoader loader = new FXMLLoader(
                                        Main.class
                                                .getResource("/newRegistration/HomePage.fxml"));
                    AnchorPane page = (AnchorPane) loader
                                        .load();
                    dialogStagee.getIcons().add(
                                        new Image("/piks/showthumb.png"));
                    dialogStagee
                                        .setTitle("WALGOTECH SOLUTIONS: 0703445354");
                    dialogStagee
                                        .initModality(Modality.APPLICATION_MODAL);
                    Scene scene = new Scene(page);
                    dialogStagee.setScene(scene);
                    HomePageController controller = loader
                                        .getController();
                    controller.setDialogStage(dialogStagee);
                    dialogStagee.setMaximized(true);
                    dialogStagee.initStyle(StageStyle.UNDECORATED);

                    conn.prepareStatement(
                        "INSERT INTO `logs`(`date`,`user`,`Terminal`,`Action`)"
                        + " VALUES ('"
                        + (java.time.LocalDate.now()
                        + " "
                        + java.time.LocalTime.now().getHour()
                        + ":"
                        + java.time.LocalTime.now().getMinute()
                        + ":" + java.time.LocalTime.now().getSecond())
                        + "',"
                        + "'"
                        + DbConnector.getMyVariablepatientvameloginCategory()
                                                        .split("as")[1]
                        + "',"
                        + "'"
                        + StartPageController.hostname
                        + "',"
                        + " 'Logged in into Registration')")
                                        .execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }   
            }
        });

        return null;
    }
};
pForm.activateProgressBar(task);

task.setOnSucceeded(event -> {

    dialogStagee.show();

    try {

    } catch (Exception e) {

    }
    pForm.getDialogStage().close();
});

pForm.getDialogStage().show();
dialogStage.close();

// dialogStage.setDisable(true);
Thread thread = new Thread(task);
thread.start();

我希望这可以帮助你解决问题。加油!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接