不在 FX 应用程序线程上; currentThread = AWT-EventQueue-0
Not on FX application thread; currentThread = AWT-EventQueue-0
因此,如果我按 CTRL + Alt + D(我正在使用 jkeymaster),我会尝试让我的 JavaFX 应用程序可见。但是每次我写 Stage.show();在我的 HotKeyListener 中,我得到 Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0 (line 7)
(我还测试了在我的热键侦听器中和侦听器外部显示文件选择器,如果我做第二件事,我不会出错)。而且,如果我只是将 System.out.println("Test")
放在我的热键侦听器中而没有其他东西,它只会输出它并且我没有收到错误
public class Main extends Application {
public static Stage s;
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(FXMLLoader.load(getClass().getResource("Main.fxml")));
stage.setScene(scene);
stage.setTitle("Test");
stage.setResizable(false);
s = stage;
}
}
控制器:
public class Controller {
public void initialize() {
Provider provider = Provider.getCurrentProvider(true);
openSaveDialog(Main.s); //No error
HotKeyListener l = hotKey -> {
Main.s.show();
openSaveDialog(Main.s);
//Returns an error
};
provider.register(KeyStroke.getKeyStroke("control alt D"), l);
}
public File openSaveDialog(Stage s) {
FileChooser chooser = new FileChooser();
chooser.setTitle("Select the output");
return chooser.showSaveDialog(s);
}
}
如果您尝试在对 Platform.runLater()
的调用中包装 HotKeyListener
的内容,这应该可以解决问题。由于您要修改 JavaFX 场景图,因此必须在应用程序线程上完成这项工作。
HotKeyListener l = hotKey -> {
Platform.runLater(() -> {
Main.s.show();
openSaveDialog(Main.s);
});
};
不过,您实际上应该通过 JavaFX 的事件注册一个密钥侦听器,而不是使用 AWT HotKeyListener。那么你就不需要调用 Platform.runLater()
因此,如果我按 CTRL + Alt + D(我正在使用 jkeymaster),我会尝试让我的 JavaFX 应用程序可见。但是每次我写 Stage.show();在我的 HotKeyListener 中,我得到 Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0 (line 7)
(我还测试了在我的热键侦听器中和侦听器外部显示文件选择器,如果我做第二件事,我不会出错)。而且,如果我只是将 System.out.println("Test")
放在我的热键侦听器中而没有其他东西,它只会输出它并且我没有收到错误
public class Main extends Application {
public static Stage s;
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(FXMLLoader.load(getClass().getResource("Main.fxml")));
stage.setScene(scene);
stage.setTitle("Test");
stage.setResizable(false);
s = stage;
}
}
控制器:
public class Controller {
public void initialize() {
Provider provider = Provider.getCurrentProvider(true);
openSaveDialog(Main.s); //No error
HotKeyListener l = hotKey -> {
Main.s.show();
openSaveDialog(Main.s);
//Returns an error
};
provider.register(KeyStroke.getKeyStroke("control alt D"), l);
}
public File openSaveDialog(Stage s) {
FileChooser chooser = new FileChooser();
chooser.setTitle("Select the output");
return chooser.showSaveDialog(s);
}
}
如果您尝试在对 Platform.runLater()
的调用中包装 HotKeyListener
的内容,这应该可以解决问题。由于您要修改 JavaFX 场景图,因此必须在应用程序线程上完成这项工作。
HotKeyListener l = hotKey -> {
Platform.runLater(() -> {
Main.s.show();
openSaveDialog(Main.s);
});
};
不过,您实际上应该通过 JavaFX 的事件注册一个密钥侦听器,而不是使用 AWT HotKeyListener。那么你就不需要调用 Platform.runLater()