[英]IllegalStateException when using JavaFX Alert in init() because not on FX application thread
Using this approach I am trying to implement an application preloader for my JavaFX application.使用这种方法,我正在尝试为我的 JavaFX 应用程序实现一个应用程序预加载器。 I want to load some heavy stuff in
init()
which may throw an exception and then continue with start()
.我想在
init()
加载一些可能引发异常的重物,然后继续start()
。 To handle the exceptions I am showing an alert using new Alert(AlertType.ERROR).showAndWait();
为了处理异常,我使用
new Alert(AlertType.ERROR).showAndWait();
显示警报new Alert(AlertType.ERROR).showAndWait();
that shows some details to the user.向用户显示一些细节。
public class Test extends Application {
@Override
public void init() throws Exception {
try {
// dome some heavy stuff here
throw new Exception();
} catch (Exception e) {
new Alert(AlertType.ERROR).showAndWait();
Platform.exit();
}
}
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
But this results in the alert not showing up and generating the following stack trace (see full stacktrace here ):但这会导致警报未显示并生成以下堆栈跟踪(请参阅此处的完整堆栈跟踪):
Exception in Application init method
java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.RuntimeException: Exception in Application init method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:895)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = JavaFX-Launcher
at javafx.graphics/com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:291)
...
at javafx.controls/javafx.scene.control.Alert.<init>(Alert.java:222)
at src/gui.Test.init(Test.java:18)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:824)
... 2 more
Exception running application gui.Test
However my approach works fine if I move the howl code from init()
to start()
.但是,如果我将嚎叫代码从
init()
到start()
我的方法就可以正常工作。
In the past I've been able to solve this kind of issues with wrapping the logic in a Platform.runLater() call.过去,我已经能够通过将逻辑包装在 Platform.runLater() 调用中来解决此类问题。 ie:
IE:
try {
[...]
} catch (Exception e) {
Platform.runLater(new Runnable() {
@Override public void run() {
new Alert(AlertType.ERROR).showAndWait();
Platform.exit();
});
}
I use this approach every time I need to do some work that's not directly related to what's going on in the interface.每次我需要做一些与界面中发生的事情没有直接关系的工作时,我都会使用这种方法。
As taken from the wiki :取自维基:
public static void runLater(Runnable runnable)
public static void runLater(Runnable runnable)
Parameters:参数:
runnable - the Runnable whose run method will be executed on the JavaFX Application Threadrunnable - 其运行方法将在 JavaFX 应用程序线程上执行的 Runnable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.