英文:
Not on FX application thread; currentThread = AWT-EventQueue-0
问题
所以我试图让我的JavaFX应用程序在按下CTRL + Alt + D时可见(我正在使用[jkeymaster][1])。但每次我在我的HotKeyListener中写入Stage.show();时,我都会得到“线程中的异常“AWT-EventQueue-0”java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0(第7行)”(我还测试过在我的热键监听器中和监听器外显示文件选择器,如果我做第二件事,就不会出现错误)。而且,如果我只在我的热键监听器中放入“System.out.println("Test")”而没有其他的东西,它只会输出它,我不会出现错误。
```java
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;
}
}
Controller:
public class Controller {
public void initialize() {
Provider provider = Provider.getCurrentProvider(true);
openSaveDialog(Main.s); //没有错误
HotKeyListener l = hotKey -> {
Main.s.show();
openSaveDialog(Main.s);
// 返回错误
};
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);
}
}
<details>
<summary>英文:</summary>
So I'm trying to make my JavaFX Application visible, if I press CTRL + Alt + D (I'm using [jkeymaster][1]). But everytime I write Stage.show(); in my HotKeyListener I get `Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0 (line 7)` (I also tested to show a file chooser in my hot key listener and outside of the listener and if I do the second thing I get no error). And also if I just put `System.out.println("Test")` in my hot key listener without the other things it just outputs it and I get no error
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;
}
}
Controller:
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);
}
}
[1]: https://github.com/tulskiy/jkeymaster
</details>
# 答案1
**得分**: 1
如果您尝试将`HotKeyListener`的内容包装在对`Platform.runLater()`的调用中,这应该可以解决问题。因为您正在修改JavaFX场景图,所以这项工作必须在应用程序线程上完成。
```java
HotKeyListener l = hotKey -> {
Platform.runLater(() -> {
Main.s.show();
openSaveDialog(Main.s);
});
};
不过,与其使用AWT HotKeyListener,您真的应该通过JavaFX的事件来注册一个键盘监听器。那样的话,您就不需要调用Platform.runLater()
了。
英文:
If you try wrapping the contents of the HotKeyListener
in a call to Platform.runLater()
this should fix it. Since you're modifying the JavaFX Scene graph, this work must be done on the Application thread.
HotKeyListener l = hotKey -> {
Platform.runLater(() -> {
Main.s.show();
openSaveDialog(Main.s);
});
};
Although, instead of using the AWT HotKeyListener, you should really register a key listener through JavaFX's events. Then you wouldn't need to call Platform.runLater()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论