如何在javafx中创建线程
我正在开发我的第一个应用程序,我使用javafx作为我的GUI部分。 在此代码中,按下按钮时,我正在尝试连接到邮件服务器:如何在javafx中创建线程,java,javafx,Java,Javafx,我正在开发我的第一个应用程序,我使用javafx作为我的GUI部分。 在此代码中,按下按钮时,我正在尝试连接到邮件服务器: public class SettingWindowController implements Initializable { @FXML TextField hostTextField; @FXML TextField loginTextField; @FXML TextField passwordTextField;
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();
}
}
也许我编写此应用程序是错误的:)您没有从FX GUI线程更新GUI,这是不允许的。您需要使用以下各项:
Platform.runLater(new Runnable() {
@Override
public void run() {
//Update your GUI here
}
});
进一步阅读,请看一看问题在于。要将更改GUI的操作重定向回FX用户线程,请调用。你可能还想看看
还有另一种方法可以将线程添加到FX用户线程,但这似乎是JavaFX中的一个bug,因此将来可能会被修补。谢谢您的回复
Platform.runLater(new Runnable() {
@Override
public void run() {
//Update your GUI here
}
});