1

I need to update jProgressBar in method which read from file and do some operations. I tried to update progress bar by this method:

 public void progressUpdate(int percent) {
     System.out.println("Update = "+percent);
     synchronized (jMainProgressBar) {
         jMainProgressBar.setValue(percent);
     }
     SwingUtilities.invokeLater(
         new Runnable() {

             public void run() {
             jMainProgressBar.updateUI();
             jMainProgressBar.repaint();
             }
         });
 }

how ever this works only then when method is done. But if i continuously updating by this method then nothing happens.

Maybe some know how to improve this method?

It also would be nice for more suggestion Worker thread and else.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Streetboy
  • 4,351
  • 12
  • 56
  • 101

4 Answers4

3

You probably want to do

public void progressUpdate(final int percent) {
     SwingUtilities.invokeLater(new Runnable() {
         public void run() {
             jMainProgressBar.setValue(percent);
         }
     });
}
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 2
    +1 yes - never access a Swing component from a non-swing thread. – Rob I May 14 '12 at 17:09
  • Yep. That pretty much sums it up :-) – aioobe May 14 '12 at 17:10
  • Still this don't do the trick. Because in my method it don't update progress. It updates just when it finishes – Streetboy May 14 '12 at 17:12
  • "never access a Swing component from a non-swing thread"? really? I have no problem updating a JProgressBar from my custom thread. I only ever have problems when I update GUIs from the event dispatch thread. – Mark Jeronimus May 14 '12 at 17:18
  • 2
    It may seem like you don't have any problems updating a JProgressBar from your custom thread, but the code may be buggy and cause deadlocks etc on a different platform. You should only manipulate the GUI from the EDT. – aioobe May 14 '12 at 18:09
2

Don't use Thread. Use Timer. Refer the following:

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • If you drop the RoseIndia link, lift the bottom link to the top & add context to the links, I could up-vote that. Oh heck - +1 anyway. – Andrew Thompson May 14 '12 at 17:24
  • Thanks Andrew :) But, what is the matter with roseindia link? – PeakGen May 14 '12 at 17:31
  • 1
    Rose India. Whatever is not wrong, is either outdated or poorly formatted & expressed. Truth is, I don't even follow those links anymore to satisfy my (minimal) curiosity as to which problems apply to any particular page. But then, if the OP cannot figure it from the 2 Oracle links and posting an SSCCE, they are lost in any case. (shrugs) – Andrew Thompson May 14 '12 at 17:37
1

Based on the comments you provided (but not from the question!) you are performing heavy work on the Event Dispatch Thread (EDT). This blocks that thread and avoids any scheduled repaints to be performed. That is why you only see the update of your JProgressBar after the work is finished, as that is the moment the EDT becomes available to perform the repaint.

The solution is already provided in the links posted by others but it basically comes down to:

  • perform the work on a worker thread
  • update the progress on the JProgressBar on the EDT

The two most common ways to achieve this are using a SwingWorker or using SwingUtilities.invokeLater from the worker thread.

All relevant links can be found in the answer of Yohan Weerasinghe

Robin
  • 36,233
  • 5
  • 47
  • 99
0

Check this out

 Timer barTimer;
 barTimer=new Timer(100,new ActionListener()
    {
     public void actionPerformed(ActionEvent e)
       {
        barvalue++;
        if(barvalue>jProgressBar1.getMaximum())
            {
                /* 
                 * To stop progress bar when it reaches 100 just write barTime.stop() 
                 */
               barTimer.stop();
               barvalue=0;

     }
    else
    {
        int a=(int)jProgressBar1.getPercentComplete();
        jProgressBar1.setStringPainted(true);
        jProgressBar1.setValue(barvalue);

      }
   }
    });
    barTimer.start();

Check the code at this link http://java23s.blogspot.in/2015/10/how-to-implement-progress-bar-in-java.html