Working with progress bar ( JProgressBar
) I came across a problem with PropertyChangeListener
. In fact, not necessarily in PropertyChangeListener
, but rather at the moment 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 see, it is necessary that the property to be updated is "progress", but this is not what happens. At least not initially. The property that is returned is "state", so the condition is not accepted, and the progress bar value 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;
}
}