Update JProgressBar

raf picture raf · Jul 4, 2010 · Viewed 9.5k times · Source

I can't update my progressbar... this is my code

Thread t=new Thread(new Runnable(){
        public void run(){
            int i=1;
            jProgBar.setMinimum(0);
            jProgBar.setMaximum(100);
            try {
                while(i<=100 || true){
                    jProgBar.setValue(i);
                    i++;
                    Thread.sleep(50);
                }
            }
            catch (InterruptedException ex){
                jProgBar.setValue(jProgBar.getMaximum());
            }
        }
    });
    t.start();

    .... Something code that correctly works

    t.interrupt();

The progress bar state is updated only at the end of thread. Can someone help me??

Answer

Paul Tomblin picture Paul Tomblin · Jul 4, 2010

Before the sleep, add a call to SwingUtilties.invokeLater() that spawns a thread to fire a firePropertyChange on the progressbar in the EDT.