Search code examples
javaswingmultithreadingjprogressbarrunnable

Update JProgressBar


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??


Solution

  • Thanks All. I solved in this way

    try{
           jProgBar.setIndeterminate(true);
           jProgBar.setStringPainted(true);
           jProgBar.setBorderPainted(true);
           new Thread(new Runnable() {
               public void run() {
                   ...
                   // here is code that i've to wait
                   // after this i stop my jProgressBar
                   ...
                   jProgBar.setStringPainted(false);
                   jProgBar.setBorderPainted(true);
                   jProgBar.setIndeterminate(false);
           }
           }).start();
       }
       catch(IllegalStateException ex){
           //some code
       }