Light
Light

Reputation: 185

JavaFX many task work in GUI

I ran a thread that updates the open time of the application. It works well. I've expanded the Service class. The time from this task update my GUI, textField by Platform.runLater

@Override
protected Task<Void> createTask() {
    return new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            while (!isCancelled()) {
                if (isPause == false) {

                    try {
                        Platform.runLater(() -> {
                               currentTimeInApp = currentTimeInApp + 1;
                               upPanelController.timeInApp.setText
                                    (currentTimeInApp.toString());
                            }
                        });
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                    }

                    if (isCancelled())
                        break;
                }
            }
            return null;
        }
    };
}

I would like to run a second thread which also updates GUI. I can't run the same thread. Can two independent threads be updated GUI ? Most of the information on the internet is dedicated to one topic. Thank you for every suggestion

Upvotes: 0

Views: 42

Answers (1)

mipa
mipa

Reputation: 10650

Yes, you can use as many threads as you like. You just have to make sure that you always do the GUI update via Platform.runLater.

Upvotes: 2

Related Questions