0

So I have a thread running and inside that thread, it updates my textArea and it should update my progressBar. I'd like to be able to update the progressBar by calling progressBar.setProgress(progress/totalProgress); without having to bind a task to it. Is this even possible?

Some sample/pseudocode because I can't actually upload the code that's being run...

private int progress;
private ProgressBar progressBar;
private int totalProgress;

public void init() {
    progress = 0;
    totalProgress = 10; // some max progress number
    progressBar.setProgress(progress/totalProgress);

    new Thread(new Runnable() {
        public void run() {
            try {
                startThread();
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }).start();
}


public void startThread() {
    for(int i = 0; i < someSize; i++) {
        textArea.append("some new message);
        progress++;
        progressBar.setProgress(progress/totalProgress);
    }
}

When I print progress and progressBar.getProgress inside the for loop, I can see it incrementing and actually "updating" but I can't get the progressBar UI to update.

Similar to how Swing has a JProgressBar and you can update it via progressBar.setValue(counter); inside a thread that's already doing other things, I'd like to be able to update the ProgressBar for FX inside a thread that's also running other things.

user1883614
  • 905
  • 3
  • 16
  • 30
  • Try printing out the value of progress/totalProgress – MJSG Apr 18 '15 at 21:55
  • so if my totalProgress = 8, then when I did progress.getProgress as my last line in the for loop, it worked. Got values from 0.125 -> 0.375 -> ... -> 0.875 -> 1.0 – user1883614 Apr 18 '15 at 22:01
  • 2
    Don't call `progressBar.setProgress()` off of the JavaFX UI thread, instead wrap such calls in `Platform.runLater()` (similarly for appending to the text area). Also your loop will complete very quickly because the sample task you have takes practically no time, so the user will never see any intermediate progress updates. Using a JavaFX Task is advisable for such things. – jewelsea Apr 18 '15 at 22:36
  • http://stackoverflow.com/questions/13784333/platform-runlater-and-task-javafx I saw this for the runLater... is there anyway I can avoid using another thread for the ProgressBar using a task? Basically, if I do instantiate a new Task and bind it to my progressBar, can run only include updateProgress and I don't need to start that task? – user1883614 Apr 18 '15 at 22:48
  • Presumably whatever you are doing takes time to execute (otherwise there would be no point in having a progress bar). If you have something that takes time to execute, you *must* execute it in a background thread, else you will make the UI unresponsive. Any updates to the UI *must* be invoked on the JavaFX Application Thread (using `Platform.runLater()` or using callbacks provided by the `javafx.concurrent` API). Why do you want to "avoid using another thread"? – James_D Apr 19 '15 at 01:55
  • Because one is already running so I'd rather have everything run on one thread instead of having multiple threads that I have to stop and startup. – user1883614 Apr 20 '15 at 20:10
  • _in Swing ...update it via progressBar.setValue(counter) inside a thread_ - no, you **must not** update anything visual off the EDT (though swing doesn't stub your nose into the mess you create by violating that rule, it's meaner in its reactions :-). it's exactly the same in both swing and fx. – kleopatra Sep 24 '15 at 09:19

0 Answers0