How to update jprogress bar
Asked Answered
B

2

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);
    }

}
Bergen answered 15/2, 2012 at 6:35 Comment(0)
K
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

Kerrison answered 15/2, 2012 at 8:14 Comment(0)
N
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

Nunnery answered 15/2, 2012 at 6:45 Comment(4)
I've set the maximum value of the jprogressbar but, still got the same issue.Bergen
To clarify the question, did the JProgressBar show up on the JDialog, i.e. a bar with zero progress is showing?Nunnery
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.Nunnery
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.Bergen
K
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

Kerrison answered 15/2, 2012 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.