Progress bar not updated correctly

Asked

Viewed 995 times

2

Working with progress bar (JProgressBar) I faced a problem in PropertyChangeListener. In fact, not necessarily in PropertyChangeListener, but yes at the time of returning the property being updated.

Tarefa tarefa = new Tarefa();
tarefa.addPropertyChangeListener(new PropertyChangeListener() {
     @Override
     public void propertyChange(PropertyChangeEvent evt) {
          if("progress".equals(evt.getPropertyName())){
               int progresso = (Integer) evt.getNewValue();
               barra.setValue(progresso);
          }
     }
});
tarefa.execute();

As you can notice, it is necessary that the property to be updated is "Progress", but that is not what happens. At least not initially. The property that is returned is "state", so the condition is not accepted, and the value of the progress bar is not updated.

What could be wrong?


Here is the class Tarefa:

public class Tarefa extends SwingWorker<Void, Void> {
    @Override
    protected Void doInBackground() throws Exception {
        int progresso = 0;
        setProgress(0);
        while(progresso < 100){
             progresso++;
             setProgress(progresso);
        }
        return null;
    }
}
  • What exactly is the Task class like? What does it do?

  • You are using Swing?

  • The Tarefe class is an extension of which class? And how it made you did extension?

  • Use the Debugger and put a Breakpoint on the line if("progress".equals(evt.getPropertyName())){ and check the name of all properties, some of them will be what you want, probably you will deduce which is by name.

  • The Task class extends from Swingworker: public class Task extends Swingworker<Void, Void>{ protected void doInBackground() throws Exception { int progress = 0; setProgress(0); while(progress < 100){ progress++; setProgress(progress); } Return null; }

  • Using a breakpoint, the property that is returned is "state".

  • If you put a Thread.sleep(100); before the setProgress and let it run for 10 seconds, which happens?

  • Why do you need that line if("progress".equals(evt.getPropertyName())){?

  • Thread.sleep before calling setProgress causes it to delay the specified time, and soon after complete the task at once. Thread.sleep as the last command within the while makes everything work perfectly!

  • if("progress".equals(evt.getPropertyName())){ serves to find out if it is the Progress property that is being updated and update the properties bar value as well.

Show 5 more comments

2 answers

2

When executing the task, run with SwingUtilities.invokeLater(tarefa);.

Remembering that: the whole structure of Swing is not thread-safe.

  • 1

    I don’t know Swing. This answer answers what the author asked?

1

Turns out the code is calling the setProgress very fast and when the event is triggered the progress is already at 100%

If you put a Thread.sleep( 20 ); within the while will see that the event will be fired more often.

NOTE: If the code runs too fast it makes no sense to have a Jprogressbar to monitor the progress of the task.

  • 1

    Got it! And I inserted the Threade.sleep in the while and it worked perfectly. And the code is pretty simple because I’m still trying to learn how everything works to later insert it in my application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.