Search code examples
javamultithreadinguser-interfaceswingjprogressbar

JProgressBar updating values behavior (Threaded)


I cannot seem to understand why when working with certain values the JProgressBar will sort of freeze and stop making calculations. For example the code bellow will only show zero all of the time;

     ...
       int value = (100/maxGenerations)*i;       //maxGenerations = 2500
       final int barValue = value;
       SwingUtilities.invokeLater(new Runnable() {
          public void run() {
          progressBar.setValue(barValue);
        }
     });

however if with the same implementation i do the following it works (of course not accurately as is just garbage calculations):

     ...
       int value = (i/100);         //where 'i' increments until = 2500
       final int barValue = value;
       SwingUtilities.invokeLater(new Runnable() {
          public void run() {
          progressBar.setValue(barValue);
        }
     });

any ideas why?


Solution

  • If you know what the range of progress is (presumably 0 - 2500) you can tell the JProgressBar via progressBar.setMaximum(2500). This way you don't have need a barValue variable, just let the progress bar know your real progress.