简体   繁体   English

更新JProgressBar

[英]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?? 有人能帮我吗??

在睡眠之前,添加对SwingUtilties.invokeLater()的调用,该调用产生一个线程以在EDT中的进度条上触发firePropertyChange。

Use a model instead of the JProgressBar directly: 直接使用模型而不是JProgressBar:

DefaultBoundedRangeModel model = new DefaultBoundedRangeModel();
JProgressBar bar = new JProgressBar(model);

// Somewhere else, perhaps in another Thread
model.setValue(i)

The following example works fine: 以下示例正常工作:

public static void main(String[] args) throws InterruptedException {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 100);
    frame.setVisible(true);
    final DefaultBoundedRangeModel model = new DefaultBoundedRangeModel();
    frame.add(new JProgressBar(model));
    Thread t = new Thread(new Runnable() {
        public void run() {
            int i = 1;
            model.setMinimum(0);
            model.setMaximum(100);
            try {
                while (i <= 100 || true) {
                    model.setValue(i);
                    i++;
                    Thread.sleep(50);
                }
            } catch (InterruptedException ex) {
                model.setValue(model.getMaximum());
            }
        }
    });
    t.start();

    Thread.sleep(2000);

    t.interrupt();
}

The best advice for your situation is to use SwingWorker . 对您的情况最好的建议是使用SwingWorker Check out the API at http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html 查看API, 网址http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html

Override process method to update value of progress bar ( then it will be done correctly on EDT) 覆盖进程方法以更新进度条的值(然后它将在EDT上正确完成)

More info can be obtained at http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html 有关更多信息,请访问http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

Per eugener, SwingWorker is definitely what you want to be using here, or any time a long-running task is spawned that could otherwise lock up your GUI prior to completion. 对于eugener来说, SwingWorker绝对是您想要在这里使用的,或者任何时候产生长时间运行的任务,否则可能会在完成之前锁定您的GUI。 A full tutorial on using progress bars with SwingWorker is available from Sun^H^H^HOracle here: Sun ^ H ^ H ^ HOracle提供了有关使用SwingWorker进度条的完整教程:

http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html

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
   }

Putting this snippet 把这个片段

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        jProgBar.repaint();
    }
}

Between 'i++' and 'Thread.sleep()' should do the job. 在'i ++'和'Thread.sleep()'之间应该完成这项工作。 To get it compiling, mark jProgBar as 'final'. 要进行编译,请将jProgBar标记为“final”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM