When I learned how to fire events in Java, I became familiar with EventListenerList. When I create my own listeners, I write the listener so it extends EventListener, I store th
EventListenerList
has a method, getListeners(Class
, specifically for the case where you are only interested in one event type.
Here's an example of how to use it:
protected void fireChangeOccurred(Change change) {
for (ChangeListener listener:
listenerList.getListeners(ChangeListener.class)) {
listener.stateChanged(new ChangeEvent(this));
}
}
If you choose to maintain your own collection of listeners, I recommend a CopyOnWriteArrayList
.