I have a task that when done it update a swing GUI telling that is done. What I saw is that you can use done()
method or attach a PropertyChangeListener
and listen for the change to done
status.
What is better to use and why? Or are they the same?
For example, this:
public class Foo implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
if ("state".equals(evt.getPropertyName())
&& (SwingWorker.StateValue.DONE.equals(evt.getNewValue()))) {
this.updateTheGuiAndOtherThings();
}
}
}
or this:
public class W extends SwingWorker {
protected Boolean doInBackground() throws Exception {...}
protected void done() {
otherClass.updateTheGuiAndOtherThings();
}
}
In my case isn't necessary better efficiency, I ask more for correct code writing.
Is it better to use done method or a change listener on SwingWorker?
Generally speaking both ways are correct and equivalent.
However the main advantage of using PropertyChangeListener
is you can have several listeners attached to the SwingWorker which allows you to split tasks in small code units rather than have a single done()
block of code. This is useful for example if you have to update several Swing components and you want to keep those updates cohesively separate.
In addition using listeners reduces coupling between the SwingWorker and GUI components: it has no knowledge about what will happen when the background thread finishes and it's ok. By overriding done()
method this won't be true anymore.
An important thing to do - when either listening for StateValue.DONE
or overriding done()
method - is to call get()
method in order to catch and treat any exception that may be thrown during doInBackground()
processing. See this Q&A related to this point.
For the rest there is no major difference. I'd go with listeners just for scalability.
Based on @mKorbel's comments below, you might want to take a look to this topic too: Is a swingWorker guaranteed to throw a state property change event on completion? and also even recosinder use a SwingWorker. I have had no problems personally but it's good to be aware about possible bugs related to the multi-threading nature of this matter.