当按下某个按钮时,我启动了一个正在做某事的线程。如果该线程引发异常,则整个JavaFX GUI将冻结,并且无法恢复。我以为是因为未捕获到异常,所以添加了UncaughtExceptionHandler,但这也无济于事。
以防万一,我将控制台重新路由到TextArea。
知道我在做什么错吗?
初始化
@FXML
public void initialize() {
PrintStream ps = new PrintStream(new Console(txaConsole));
System.setOut(ps);
System.setErr(ps);
}
按钮按下
@FXML
public void btnConvertJson2DBReleased(Event evnt) throws Exception {
if (txfDirectory.getText().equals("")) {
System.out.println("Error: Directory is empty");
} else {
Runnable r = new Runnable() {
public void run() {
JSON2DBApplication.start("--Directory=" + txfDirectory.getText());
}
};
Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread th, Throwable ex) {
System.out.println("Uncaught exception: " + ex);
}
};
t = new Thread(r);
t.setUncaughtExceptionHandler(h);
t.start();
}
}