简体   繁体   中英

Reset Progress bar to zero after method completion java swing

I am trying to use progress bar in my swing code. It works well but i am enable to reset to zero after the execution is finished. Here is my code logic to get user inputs and call respective methods.

    final JProgressBar progressBar = new JProgressBar();

        btnRun = new JButton("Run");
        btnRun.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                btnRun.setEnabled(false);

                if (textStartDate.getText().length() == 0 ||textEndDate.getText().length() == 0 ||textField.getText().length() == 0
                        || textField_1.getText().length() == 0) {
                    JOptionPane.showMessageDialog(frame,"Please Enter all fields");
                    }
//              else if(minDate.compareTo(maxDate)>0 ){
//                      JOptionPane.showMessageDialog(frame,"Starting Date should be lesser then end Date");
//                  }
                else{
                    ArrayList<String> ss = list.listFiles(fSource,
                            textStartDate.getText(), textEndDate.getText());
                    for (String string : ss) {
                        i++;
                        progressBar.setMaximum(i);
                        System.out.println(i);

                        progressBar.setValue(i);

                        System.out.println(textField.getText().replace('\\', '/'));
                        list.writeToFolder(ftarget, string);
                    }

                    btnRun.setEnabled(true);

                }

            }

        });

Set values to 0 like next:

progressBar.setValue(0);
progressBar.setMaximum(0);
progressBar.setString("");

The main problem you're having is your running a loop within the context of the Event Dispatching Thread, which will prevent it from process, amongst other things, paint requests.

This means the the progress bar won't actually update to until you exit the actionPerformed method.

There are a number of possible solutions, but the easiest would be to use a SwingWorker , which will allow you to run the loop in a background thread, but has the ability to provide both progress updates as well as re-sync updates back to the EDT.

Take a look at Concurrency in Swing for more details

For example...

I would, also, focus on maintaining the maximum value as a static value, for example...

progressBar.setValue(0);    
progressBar.setMaximum(100);

//... Within the SwingWorker...

ArrayList<String> ss = list.listFiles(fSource,
                        textStartDate.getText(), textEndDate.getText());
for (String string : ss) {
    i++;    
    int progress = (int)(((float)i / (float)ss.size()) * 100f);
    setProgress(progress);
    //...
}

For example. It will make the progress bar actually progress, otherwise it will always appear to be 100% (because i is both the value and the maximum ).

This will automatically re-seed the progress bar the next time you create a new instance of the SwingWorker and execute it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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