【问题标题】:How to avoid Not on FX application thread; currentThread = JavaFX Application Thread error?如何避免不在 FX 应用程序线程上; currentThread = JavaFX 应用程序线程错误?
【发布时间】:2014-01-31 18:22:07
【问题描述】:

下面的代码 sn-ps 给我错误Not on FX application thread; currentThread=JavaFXApplication Thread。这个应用程序在 java 1.7 中运行良好,但是当我将它移到 fx8 时它现在给出错误。当我第一次尝试启动应用程序时,它按预期工作。但是在关闭舞台并再次打开它之后,它就不起作用了。

错误也是模棱两可的Not On fx application thread and current thread- javafxapplication thread。如果当前线程是fx应用线程,那么not on fx application thread是什么意思。

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,答案将显而易见。

标签: multithreading javafx javafx-8 javafx-2


【解决方案1】:

打电话

Platform.runLater(new Runnable(){
// do your GUI stuff here
});

会解决的。

【讨论】:

  • 如果您正在处理纯 javafx 应用程序,这将起作用。我的应用程序是 SWT,我在 fxcanvas 上创建 fx 组件。如果我在 Platform.runLater(new Runnable(){/ / ... });则工具包未初始化将被抛出。
【解决方案2】:

当我从 javafx 2 中的任务修改 UI 元素时发生这种情况,例如 listview 元素。A Task Which Modifies The Scene Graph 帮助我解决了问题,即通过更新 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;
     }
 };

【讨论】:

  • 您的解决方案也解决了我的问题。我仍然不明白为什么。谁能向我解释一下,为什么“Platform.runLater(new Runnable(){});”解决了吗?
  • @Yannic Hansen 这是因为在 javaFX 中只有 FX 线程可以修改 ui 元素。这就是你使用 Platform.runLater 的原因。
  • 这也解决了我的问题,我试图每 1 分钟更新一次窗口标题。立即生效。
【解决方案3】:

当您尝试更改某些组件 UI(例如标签文本)时,应该会发生这种情况。像这样运行总是有效的:

@FXML Label myLabel;

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      您可以在代码的任何部分更改表单或转到另一个视图或 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();
      
              }
      
          }
      
      }
      

      【讨论】:

        【解决方案6】:

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

        【讨论】:

        • 这专门解决了导致我来到这里的问题,尝试执行Thread initThread = new Thread(new Runnable() { ... scraper.init(); navButton.setText("Finished!"); ... }); initThread.start();,但我不知道其他线程试图访问 javafx 应用程序线程是不允许的(显然应该是: )。我将实现消息传递。谢谢
        【解决方案7】:

        我在尝试为我的程序添加启动画面时遇到了同样的问题。这就是我的代码的样子

        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();
        

        我希望这能帮助您解决问题。干杯。

        【讨论】:

          猜你喜欢
          • 2015-06-09
          • 1970-01-01
          • 2019-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多