Removing event listeners
If you need to remove an event listener, that has been added before, you can use the removeEventListener() method of EventDispatcher.
import net.schst.EventDispatcher.*; EventDispatcher disp = EventDispatcher.getInstance(); EventListener one = new DebugHandler("one"); disp.addListener("onLogin", one); EventListener two = new DebugHandler("two"); disp.addListener("onLogin", two); boolean removed = disp.removeEventListener("onLogin", one); if (removed == true) { System.out.println("The event listener has succesfully been removed."); } disp.triggerEvent("onLogin");
The triggered event will be only caught by the DebugHandler? named two.
There are two things that you need to remember:
- If you added an event listener more than once, only the first occurance will be removed.
- You need to keep in mind, that EventDispatcher will use the equals() method of the event listener that you passed in to compare it with the existing event listener objects. If this method returns true, the listener will be removed.
Removing global event listeners
You can also remove event listeners, that you added globally, using the removeGlobalEventListener() method.
