下面的代码片段给出了错误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);
}
答案 0 :(得分:37)
当我从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;
}
};
答案 1 :(得分:14)
Platform.setImplicitExit(false);
解决了我的问题。我认为他们改变了JavaFX 8中的实现,因此在JavaFX 2中没有任何问题的相同代码在那里给出了而不是fx应用程序线程错误。
答案 2 :(得分:14)
当您尝试更改某些组件UI(如标签文本)时,应该会发生这种情况。这样的运行总是起作用:
@FXML Label myLabel;
Platform.runLater(new Runnable(){
myLabel.setText("some text");
});
答案 3 :(得分: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();
}
}
}
答案 4 :(得分:1)
它没有在上面的代码中明确显示,但我相当确定发生的是你在应用程序(主)javafx线程之外创建一个线程,然后你试图在javafx对象上执行操作(如关闭,打开窗户等)在SECOND线程上。这是严格禁止的,因为只有主线程可以直接控制javafx对象。如果这成为程序的一个要求,你需要使用第二个线程进行其他事情,比如计算等等。你必须使用某种形式的消息传递让另一个线程知道你想做任何javafx动作。
答案 5 :(得分:0)
在尝试为程序添加启动屏幕时,我遇到了相同的问题。这就是我的代码
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();
我希望这会帮助您解决问题。干杯。