-1

I have a Progressbar on my JavaFx app. I need to update it from a different thread. That thread will have to provide the progerss value.

My code looks like this

// Task class

public class UpdateProgressBarTask extends Task<Void> {

    private double progressValue;

    public UpdateProgressBarTask(double value) {
        this.progressValue=value;
    }

    @Override
    protected Void call() throws Exception {
        this.updateProgress(this.progressValue, 100);
        return null;
    }
}

//Controller

public void createProgerssTask(double doubleValue) {
    UpdateProgressBarTask task = new UpdateProgressBarTask(doubleValue);
    progressBar.progressProperty().bind(task.progressProperty());
    new Thread(task).run();
}

then I create a new Thread on the controller and from there I have to update the progerssBar

this.timer = Executors.newSingleThreadScheduledExecutor();
this.timer.scheduleAtFixedRate(someFunction(), 0, 33, TimeUnit.MILLISECONDS);

someFunction() {
    ...
    createProgerssTask(double value);
}

I am getting the exception

Exception in thread "pool-2-thread-1" 2 java.lang.IllegalStateException: Not on FX application thread; currentThread = pool-2-thread-1

What am I doing wrong here ? Any help much appreciated

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
  • 1
    Does this answer your question? [How to avoid Not on FX application thread; currentThread = JavaFX Application Thread error?](https://stackoverflow.com/questions/21083945/how-to-avoid-not-on-fx-application-thread-currentthread-javafx-application-th) – Miss Chanandler Bong Mar 24 '20 at 13:43
  • [mcve] please .. – kleopatra Mar 24 '20 at 14:07

1 Answers1

-2

Solved, using Platform.runLater(Runnable)

//Controller
public void updateProgerssBar(double doubleValue) {         

    Platform.runLater(new Runnable() {
        @Override
        public void run() {             
            progressBar.setProgress(doubleValue);
        }           
    });     
}
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
  • typically not needed: binding the progressBar.progress to the task.progressProperty (which is guaranteed to be updated on the fx app thread) should work as-is. Most likely, there's something else wrong in the code you are not showing ;) – kleopatra Mar 24 '20 at 14:15