GUI update with threads error

穿精又带淫゛_ 提交于 2019-12-02 07:30:48

Clearly Client.run() is being executed on a background thread, and calls Controller.movePawn(), which (via refresh()) updates the UI. You cannot update the UI on a background thread. You need to wrap the code that updates the UI in Platform.runLater(). So, it's pretty hard to tell without a complete example, but using that particular sequence of method calls as an example, it looks like you need to do

public void movePawn(int x1, int y1, int x2, int y2) {
    Platform.runLater(() -> {
        Pawn pawnTemp = game.getBoard().getField(x1, y1).getPawn();
        game.getBoard().getField(x1, y1).setPawn(null);
        game.getBoard().getField(x2, y2).setPawn(pawnTemp);

        refresh();
    });
}

There are probably similar issues throughout the code, but the bottom line is that you:

  1. Must execute code that updates the UI on the FX Application Thread
  2. Must not execute code that blocks (or takes an appreciable amount of time to run) on the FX Application Thread.

One of your other classes seems to start a thread, which is not controlled by your fx application thread. Hard to tell, which one without seeing the other classes.

Please have a look at what to consider when using background threads in a java fx application (or when using libaries, that do so), e.g. how to use Task and Platform.runLater(Runnable r). There are countless other questions about that topic on SO, that might help you.

https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!