-1

So when I launch my application I show a button over my window as a loading screen/click to play button. So when the application starts, it says "Loading" and is set to disabled in the initalize method. Then my controller instantiates another class that uses 4 threads to compute a thing, this is supposed to last during the time the user sees the loading screen. So my Controller instantiates a private class which acts as a callback to the class with multiple threads. When that class is done computing, it will callback to the controller that it is finished. When this happends I want to Enable my button again and change the text so the user can click it and begin. This is part of the code:

@FXML
private Button btnStart;

private final MuliThreadedClass treeBuilderStarter = new TreeBuilderStarter(board, new CBTreeDoneImpl());


    private class CBTreeDoneImpl implements CBThreadDone{

        @Override
        public void done(ComplextObject obj) {
            btnStart.setDisable(false);
            btnStart.setText("Click to start!");
        }
    }

    @FXML
    public void initialize() {
        btnStart.setDisable(true);
    }

Now when the callback is called and

  btnStart.setDisable(false);
  btnStart.setText("Click to start!");

is executed I get an exception saying IllegalStateException not on FX thread. I understand the issue, but how would you go about fixing this?

iffe1
  • 1
  • Possible duplicate of [Javafx Updating UI from a Thread without direct calling Platform.runLater](https://stackoverflow.com/questions/27309642/javafx-updating-ui-from-a-thread-without-direct-calling-platform-runlater) – Developer66 Sep 09 '17 at 13:08
  • Please google first: https://stackoverflow.com/a/27316596/8087490 – Developer66 Sep 09 '17 at 13:08
  • Assuming your `done()` method is supposed to be executed when some background work is completed, you could also consider extending [`Task`](http://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html) and overriding the [`succeeded()`](http://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html#succeeded--) method, which is executed on the FX Application Thread. – James_D Sep 09 '17 at 13:51

1 Answers1

2

You should use Platform.runLater(Runnable) to perform GUI actions on non-FX threads.

Sample

Platform.runLater(() -> {
    // Do sth...
});
Florian Cramer
  • 1,227
  • 1
  • 16
  • 29