不在 FX 应用程序线程上;currentThread = AWT-EventQueue-0

huangapple go评论60阅读模式
英文:

Not on FX application thread; currentThread = AWT-EventQueue-0

问题

所以我试图让我的JavaFX应用程序在按下CTRL + Alt + D时可见我正在使用[jkeymaster][1])。但每次我在我的HotKeyListener中写入Stage.show();我都会得到线程中的异常AWT-EventQueue-0java.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&#39;m trying to make my JavaFX Application visible, if I press CTRL + Alt + D (I&#39;m using [jkeymaster][1]). But everytime I write Stage.show(); in my HotKeyListener I get `Exception in thread &quot;AWT-EventQueue-0&quot; 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(&quot;Test&quot;)` 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(&quot;Main.fxml&quot;)));
        stage.setScene(scene);
        stage.setTitle(&quot;Test&quot;);
        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 -&gt; {
                Main.s.show();
                openSaveDialog(Main.s);
                //Returns an error
            };
            provider.register(KeyStroke.getKeyStroke(&quot;control alt D&quot;), l);
        }
    
        public File openSaveDialog(Stage s) {
            FileChooser chooser = new FileChooser();
            chooser.setTitle(&quot;Select the output&quot;);
            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 -&gt; {
    Platform.runLater(() -&gt; {
        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()

huangapple
  • 本文由 发表于 2020年9月11日 20:13:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63846883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定