Search code examples
javaswingvectoreventlistenerlist

Advantage of EventListenerList over using Vector


what is the advantage of using EventListnerList over Vector ? Which is more preferred ?


Solution

    • Vector is pseudo "deprecated" in favor of the collections API List.
    • A List may contain whatever you want it to without restriction
    • While it would be possible to support multiple different listeners within a List, it's management would be complicated, as you'd need to walk the list each time you wanted to find a given type of listener. You'd be better of using some kind of Map, but then, you'd be duplicating what EventListenerList does...
    • EventListenerList will allow you to manage multiple different types of EventListeners, but is restricted to managing only class that implement the EventListener interface
    • Swing components expose listenerList which is an instance of EventListenerList as a protected field, so you don't need to create your own

    So, I would suggest, you might use EventListenerListener when...

    • You're extending from a Swing based component, as it's simpler and is already available...
    • Managing multiple different types of listeners
    • When your listeners implement EventListener

    You might consider using a List when...

    • You only need to deal with a single type of listener and
    • You aren't already extending from a Swing based component...as it provides access to the EventListenerList any way...