为什么我的ProgressBar没有在for()循环中更新?

时间:2015-08-20 10:34:31

标签: java multithreading swing loops updating

我有一个JProgressBar progressBar,它应该在for()循环中更新。实际上我已经看过这个问题了:Progress bar not updating during a loop我尝试了一个新的线程,但我不知道为什么它仍然没有更新。

我尝试了什么:

    public void getNewUUID(BufferedWriter output) {
    Menu.progressBar.setMinimum(0);
    Menu.progressBar.setMaximum(100);
    String hashchar = "";
    x = ID_LENGTH/100;
    y=0;

    for(int ch = 0; ch != ID_LENGTH; ch++) {
        done = ch;
        hashchar = "";
        for(int id = 0; id < ID_LENGTH; id++) {
            hashchar = hashchar+ALPHA_CHARS[rnd.nextInt(ALPHA_CHARS.length)];

            try {
                output.write(hashchar);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            hashchar = "";

            new Thread(new Runnable() {
                public void run() {
                    if(done>=x) {
                        x=x+x;
                        y++;
                        Menu.progressBar.setValue(y);

                    }
                }
            }).start();
        }



    }

try {
    output.flush();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
try {
    output.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

2 个答案:

答案 0 :(得分:3)

您正在非UI线程上执行进度条更新。您将需要使用SwingUtilities.invokeLater(Runnable r)

chrome-extension://namhfjepbaaecpmpgehfppgnhhgaflne/content/web/viewer.html?file=http%3A%2F%2Fwww.ifets.info%2Fjournals%2F10_4%2F9.pdf

这应该确保进度条更新发生在UI线程上,这应该会导致进度条刷新新的价值观。

答案 1 :(得分:1)

根据以下类似问题:Example 1Example 2Example 3Example 4Example 5

最好的办法是在SwingWorker创建的后台线程中执行长时间运行的任务,并在worker内部,在代码运行时设置其progress属性。然后,Swing应用程序可以使用PropertyChangeListener监视工作进程的状态,并在侦听器中设置JProgressBar进度的值。也许类似的东西:

public void getNewUUID(BufferedWriter output) {
    // JProgressBar should not be a static field
    Menu.progressBar.setMinimum(0);
    Menu.progressBar.setMaximum(100);
    x = ID_LENGTH / 100;
    y = 0;
    MyWorker myWorker = new MyWorker(output);
    myWorker.addPropertyChangeListener(new MyWorkerListener());
    myWorker.execute();
}

private class MyWorkerListener implements PropertyChangeListener {
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress".equals(evt.getPropertyName())) {
            int progress = (int) evt.getNewValue();

            // TODO: set your JProgressBar's value here  *********
        }

        if (SwingWorker.StateValue.DONE == evt.getNewValue()) {
            MyWorker myWorker = (MyWorker) evt.getSource();
            try {
                myWorker.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}

private class MyWorker extends SwingWorker<Void, Void> {
    private BufferedWriter output;

    public MyWorker(BufferedWriter output) {
        this.output = output;
    }

    @Override
    protected Void doInBackground() throws Exception {
        String hashchar;
        for (int ch = 0; ch != ID_LENGTH; ch++) {
            done = ch;
            hashchar = "";
            for (int id = 0; id < ID_LENGTH; id++) {
                hashchar = hashchar
                        + ALPHA_CHARS[rnd.nextInt(ALPHA_CHARS.length)];
                try {
                    output.write(hashchar);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                hashchar = "";
                if (done >= x) {
                    x = x + x;
                    y++;
                    setProgress(y);
                }
            }
        }
        try {
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}