Property Change Listeners : Adding and Removing
10 May 2007Quick lesson learned.
If you have a property change listener registered against a specific property name, you have to use removePropertyChangeListener(String, PropertyChangeListener) and not removePropertyChangeListener(PropertyChangeListener) when you want to remove it.
Basically, you cannot do the following:
PropertyChangeListener foo = new XXX();
public void enable()
{
addPropertyChangeListener("SomeMessage", foo);
}
public void disable()
{ removePropertyChangeListener(foo);
}
Truth be told it’s rather innocent looking code but I did spend a good day tracking it down through some fairly nasty table and panel hierarchies. The kicker is that by not actually removing the property change listener, you ended up queuing many copies of the exact same listener (basically enable() / disable() were called repeatedly based on the state of the UI).
To make a bad situation worse, have the listener make a server call. It’s not too bad when there’s only one registered, multiple that by 300 and you’ve got issues.