Search code examples
javaswingswingworker

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. 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. 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.

I have tried using publis process but it does not show the update only a full bar at the end. 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))+"%");            
    }

Solution

  • 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();