This question already has answers here:
Using threads to make database requests
                                
                                    (3个答案)
                                
                        
                                4年前关闭。
            
                    
我正在开发我的第一个应用程序,并且在我的GUI部分使用javafx。
在这段代码中,当按下按钮时,我试图连接到邮件服务器:

public class SettingWindowController implements Initializable {
    @FXML
    TextField hostTextField;
    @FXML
    TextField loginTextField;
    @FXML
    TextField passwordTextField;
    @FXML
    TextField portTextField;
    @FXML
    Label statusConnectionOK;
    @FXML
    ChoiceBox choiceStore;

    public void pressConnect(ActionEvent actionEvent) throws InterruptedException {
        Task task = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                saveProperty();
                CheckMailConnect connect = new CheckMailConnect();
                if (connect.checkConnect()) {
                    statusConnectionOK.setText("");
                    statusConnectionOK.setStyle("-fx-text-fill:green;");
                    statusConnectionOK.setText("Connection OK");
                } else {
                    statusConnectionOK.setText("");
                    statusConnectionOK.setStyle("-fx-text-fill:red;");
                    statusConnectionOK.setText("Connection failed");
                }
                return null;
            }
        };
        Thread th = new Thread(task);
        th.setDaemon(true);
        th.start();
    }
}


但是我有这个问题:


  线程“ Thread-4”中的异常java.lang.IllegalStateException:不
  在FX应用程序线程上; currentThread =线程4
        在com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:204)
        在com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:438)
        在javafx.scene.Parent $ 2.onProposedChange(Parent.java:364)
        在com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:113)
        在com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:108)
        在com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:575)
        在com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(LabeledSkinBase.java:204)
        在com.sun.javafx.scene.control.skin.LabelSkin.handleControlPropertyChanged(LabelSkin.java:49)
        在com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda $ registerChangeListener $ 61(BehaviorSkinBase.java:197)
        在com.sun.javafx.scene.control.skin.BehaviorSkinBase $$ Lambda $ 122 / 82162992.call(未知
  资源)
        在com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler $ 1.changed(MultiplePropertyChangeListenerHandler.java:55)
        在javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
        在com.sun.javafx.binding.ExpressionHelper $ SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
        在com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
        在javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:103)
        在javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:110)
        在javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:144)
        在javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:49)
        在javafx.beans.property.StringProperty.setValue(StringProperty.java:65)
        在javafx.scene.control.Labeled.setText(Labeled.java:145)
        在com.helpdesk.gui.controller.SettingWindowController $ 1.call(SettingWindowController.java:41)
        在com.helpdesk.gui.controller.SettingWindowController $ 1.call(SettingWindowController.java:33)
        在javafx.concurrent.Task $ TaskCallable.call(Task.java:1423)
        在java.util.concurrent.FutureTask.run(FutureTask.java:266)
        在java.lang.Thread.run(Thread.java:745)


请告诉我,如何解决此错误?

谢谢专家。

但是我不确定如何在我的应用程序中做到这一点。这是我从中打开新窗口的控制器:

public class Controller implements Initializable {
    @FXML
    private MenuItem itemSetting;
    @FXML
    private Stage stage;
    @FXML
    private Parent root;

    @FXML
    public void pressSettingItem(ActionEvent actionEvent) throws IOException {
        if (actionEvent.getSource() == itemSetting) {
            stage = new Stage();
            FXMLLoader loader = new FXMLLoader();

            loader.setLocation(Controller.class
                                         .getResource("/SettingsWindows.fxml"));

            root =  loader.load();
            stage.setScene(new Scene(root, 300,300));
            stage.setTitle("Setting mode");
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.showAndWait();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {}
}


也许我写这个应用程序是错误的:)

最佳答案

问题是only a JavaFX thread is allowed to change GUI-elements。若要将更改GUI的操作重定向到FX用户线程,请调用Platform.runLater(Runnable r)。您可能还需要查看this question

还有另一种方法可以将线程创建到FX用户线程,但这似乎是JavaFX中的错误,因此将来可能会被修补。

关于java - 如何在javafx中创建线程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31219165/

10-15 11:32