2

I have a simple JForm and a JDialog in my application. JDialog box contains a JProgressBar eliment and I'put a method in the JDialog as,

 public void updateProgress(int val){
        prgProgress.setValue(val); //prgProgress-> name of the JProgressBar
    }

to update the progress bar.

When I try to update the progress bar in my JDialog from the JForm, JProgressBar doesn't update as expected please tell me what could be the error,

Ex.

public class Status extends javax.swing.JDialog{
 private javax.swing.JProgressBar prgProgress = new javax.swing.JProgressBar;
.....

public void updateProgress(int val){
            prgProgress.setValue(val); //prgProgress-> name of the JProgressBar
        }

.....

}

public class MyForm extends javax.swing.JInternalFrame {
    Status s = new Status();
    s.setVisible(true);
    for(int i=0; i<100; i++){
        s.updateProgress(i);
    }

}
Harsha
  • 3,548
  • 20
  • 52
  • 75

2 Answers2

1

Firstly, I can't see from your code above showing that you have added the JProgressBar into the JDialog, but I assume you have done so. Secondly, you didn't set the maximum value of the JProgressBar, from either constructor or member function setMaximum(). This might be a reason of showing no progress.

See Also:

http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html

Victor Wong
  • 2,486
  • 23
  • 32
  • I've set the maximum value of the jprogressbar but, still got the same issue. – Harsha Feb 15 '12 at 06:49
  • To clarify the question, did the JProgressBar show up on the JDialog, i.e. a bar with zero progress is showing? – Victor Wong Feb 15 '12 at 06:58
  • Then, try to use a Thread to set the JDialog visible. It's because JDialog will block the current thread which should be owned by the Form after it is set to visible. – Victor Wong Feb 15 '12 at 07:11
  • OK I've found the error, I have make the JDialogs' setModel=true, once I removed that line progress is displayed fine. but I wonder why it is not updated when setModel=true is true. – Harsha Feb 15 '12 at 07:15
1

You have a issue with Concurency in Swing, Swing is single threaded and nothing happends if you update GUI out of EDT, you have to implement SwingWorker for updating JProgressBar, example with descriptions here

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319