How to update jprogress bar

梦想的初衷 提交于 2019-12-10 23:56:43

问题


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

}

回答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




回答2:


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



来源:https://stackoverflow.com/questions/9288923/how-to-update-jprogress-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!