Question

I have read the docs for PropertyChange support and EventListenerList. From my understanding, both serve similar purposes in holding a list of listeners and notifying them when event/propertyChange occurs. Is it only in case of GUI applications, that EventListenerList becomes handy?. For a simple JavaBean application that does not use GUI components, is there an advantage of using one over the other.

Was it helpful?

Solution

Generally speaking, a PropertyChangeEvent occurs when some property value of the object changes (a property/value which you can read), where a (general) event could describe any kind of event (such as a change in selection or a mouse click), it doesn't have to represent a change in the state of the object

PropertyChangeSupport is part of the bean framework (in particular, but not limited to) GUI editors. This doesn't mean you can't use it, in fact, many objects rely on this functionality, such as SwingWorker and many of the objects from SwingLabs for example.

With that in mind, you should use ProperyChangeSupport when you want to notify interested parties that a property/value of an object changes and EventListenerList when you want to provide general event notification for things that are occurring within the object (but don't have to be related to a specific property or state)

The only issue I have with ProptertyChanegSupport, is it can less obvious which properties are bound and which aren't, sometimes making it difficult to get started with new objects, where as it's reasonably easy to look up all the "addListener" methods, but that's just me

While I'm sure the original intention of the EventListenrerList was for GUIs, I've used them for non-GUI work before, but you might find it easier to to use a List if you only have support for a single listener though, just saying

OTHER TIPS

as PropertyChangeListener is just a specific "subclass" (extending interface) of EventListener - which is a marker interface and defines no methods, it is much easier to work with PropertyChangeSupport than EventListenerList - that is because if you start off with an EventListenerList you'll need to always do instanceof checks and casting to get to the actual "business" methods of your listeners (since the interface they all implement has no methods)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top