简体   繁体   English

如何在for循环中将JavaProgressBar与SwingWorker结合使用?

[英]How to use JavaProgressBar with SwingWorker in a for loop?

I the following code: 我下面的代码:

    for (int iteration = 0; iteration < iterations; iteration++) {
                    if(iteration==0){jProgressBar1.setValue(0);}
        jProgressBar1.setValue((int)(((iteration+1.)/iterations)*100));
                    jProgressBar1.setString(Integer.toString((int)(((iteration+1.)/iterations)*100))+"%");

and then come other steps and function calls. 然后执行其他步骤和函数调用。 As it is now the ProgressBar has the value 0 at the start of the application and 100 when the for loop ends. 现在, ProgressBar在应用程序开始时的值为0,在for循环结束时的值为100。 I've looked over the tutorials and questions already asked but I don't know how to configure the SwingWorker functions to match my case. 我查看了已经问过的教程和问题,但是我不知道如何配置SwingWorker函数以匹配我的情况。 For example i don't know what doInBackground() should contain since the value of the bar depends only of the iterations and not on the results of the computations done in the loop. 例如,我不知道doInBackground()应该包含什么,因为柱的值仅取决于迭代,而不取决于循环中完成的计算结果。

I have tried using publis process but it does not show the update only a full bar at the end. 我尝试使用publis process但最后未显示完整的更新。 This is the code now: 这是现在的代码:

public class Algorithm extends SwingWorker<Void,Integer>{
    //variable declarations
    public void Run(MDVRPData data,String fileName, JTextArea jText, boolean method1,
            boolean method2, boolean method3, boolean twoOpt,
            boolean oneOneExc, boolean oneZeroExc, JSpinner iter, final JProgressBar jProgressBar1) {
        iterations=(Integer) iter.getValue();
        jText.setText("");
        jText.append("START ALGORITHM \n");
        jProgressBar1.setStringPainted(true);
        initDrawWindow(fileName);
        dr = new DecodeRoutes(twoOpt,oneOneExc,oneZeroExc);
        this.data=data;
        this.oneOneExc=oneZeroExc;
        this.twoOpt=twoOpt;
        this.oneZeroExc=oneZeroExc;
        this.bar=jProgressBar1;
        doInBackground();

}
    @Override
    protected Void doInBackground() {
        //initialization code
        for (int iteration = 0; iteration < iterations; iteration++) {
            publish(iteration);
            //other computations and function calls
        }
        //function calls
        return null;
    } 
    @Override
    protected void process(List<Integer> iter) {
        int iteration=iter.get(iter.size()-1);
        bar.setValue((int)(((iteration+1.)/iterations)*100));
        bar.setString(Integer.toString((int)(((iteration+1.)/iterations)*100))+"%");            
    }

If you want to update your progress bar from the doInBackground method you should enclose your update code in SwingUtilities.invokeLater or invokeAndWait method. 如果要从doInBackground方法更新进度条,则应将更新代码放在SwingUtilities.invokeLater或invokeAndWait方法中。

Something like this: 像这样:

for (int iteration = 0; iteration < iterations; iteration++) {
  final int itr = iteration;              
  SwingUtilities.invokeLater(new Runnable() {
      public void run() {
         if(itr==0) {
           jProgressBar1.setValue(0);
         } else {
           jProgressBar1.setValue((int)(((itr+1.)/itr)*100));
           jProgressBar1.setString(Integer.toString((int)(((itr+1.)/itr)*100))+"%");
         }
      }
  });
}

One of my professors helped me solve the problem. 我的一位教授帮助我解决了这个问题。 Here is the code: 这是代码:

public class Algorithm extends SwingWorker<Void,Integer>{
    //variable declarations
    public Algorithm(MDVRPData data,String fileName, JTextArea jText, boolean method1,
        boolean method2, boolean method3, boolean twoOpt,
        boolean oneOneExc, boolean oneZeroExc, JSpinner iter, final JProgressBar jProgressBar1) {
        iterations=(Integer) iter.getValue();
        jText.setText("");
        jText.append("START ALGORITHM \n");
        jProgressBar1.setStringPainted(true);
        initDrawWindow(fileName);
        dr = new DecodeRoutes(twoOpt,oneOneExc,oneZeroExc);
        this.data=data;
        this.oneOneExc=oneZeroExc;
        this.twoOpt=twoOpt;
        this.oneZeroExc=oneZeroExc;
        this.bar=jProgressBar1;
        doInBackground();
    }  
    @Override
    protected Void doInBackground() {
         //initialization code
        for (int iteration = 0; iteration < iterations; iteration++) {
            publish(iteration);
            //other computations and function calls
        }
        //function calls
        return null;
    }  
@Override
protected void process(List<Integer> iter) {
    int iteration = iter.get(iter.size() - 1);
    int procent = (int) (((iteration + 1.) / iterations) * 100);
    bar.setValue(procent);
    bar.setString(procent + "%");
}

And it is called like so: 它的名称如下:

    alg=new Algorithm(myData, fileName, jRadioButton1.isSelected(), jRadioButton2.isSelected(), jRadioButton3.isSelected(),
            jRadioButton4.isSelected(), jRadioButton5.isSelected(), jRadioButton6.isSelected(), (Integer)jSpinner1.getValue(), jProgressBar1);
    alg.execute();

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

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