 Of course
you add a PropertyChangeListener to a SwingWorker's list of listeners to be informed of advances in the progress
of computations and to notice changes in SwingWorker's status.
But, when do you remove those listeners? Of course if you don't remove listeners then you keep references
from the SwingWorker to the listeners and that may lead to memory leaks.
Of course the safest place to remove listeners is when the SwingWorker is done. Something like this:
public void propertyChange(java.beans.PropertyChangeEvent propertyChangeEvent)
{
SwingWorker source = (SwingWorker) propertyChangeEvent.getSource();
if ( "state".equals( propertyChangeEvent.getPropertyName() )
&& (source.isDone() || source.isCancelled() ) )
{
source.removePropertyChangeListener( this );
}
}
So the listener removes itself when the SwingWorker is done. Easy and clean. So here we go with
another suggestion for SwingWorkers:
SwingWorker suggestion IV: Clean up listeners when done
Each PropertyChangeListener should remove itself from the list of listeners whenever the SwingWorker is done.
Enough for today. Keep swinging,
Antonio
|