An Amendment to the JavaBeans Specification
Hans Muller and Mark Davidson
<!-- @@@ print version @@@ -->Presently all of the state in an AWT component that can be written can also be read, e.g. there are no write-only properties in the component API. Event listeners are a notable exception. AWT event listeners are managed according to the JavaBeans conventions with a pair of methods: addFooListener() and removeFooListener() for a listener that implements the FooListener interface.
No access is provided to the listener lists themselves. The fields that contain the listener lists may be private, package private or protected and no methods are provided to return the contents of the listener lists. This has caused some problems for Swing and other AWT clients:
- The Swing UI classes have to keep references to every listener they add, just to enable removing them if the UI is changed. Swing applications contain 1000's of references like this. In general, you can't clear any JavaBeans listener list unless you've kept a private copy.
- Archiving systems have to resort to implementation dependent snooping to discover the contents of listener lists.
- Externalization isn't an option for classes derived from Component, like the Swing components, because the listeners are inaccessible.
To mitigate the problem in Java 2 Standard Edition (J2SE) v 1.3 we added a getListeners(Class) method to Component and to the Swing classes that defined listener lists. The getListeners(Class) method uses a Class as its argument to specify a particular listener list. For example to get all of the listeners added with addFocusListener(), one would write: getListeners(FocusListener.class).
This particular approach to exposing listener lists was taken to minimize the overall change to the AWT/Swing public API. It was not indented to be a pattern for all JavaBeans and it did not handle PropertyChangeListeners - which can be added to a single property, as in addPropertyChangeListener("myProperty", myListener).
The specific implementation of this work in the AWT and Swing is covered by 4290704.
Solution
The JavaBeans Specification has been extended so that listener lists can optionally be read. Two extensions have been added:
- To the add/remove pattern for listeners: add an optional
get<ListenerType>s()method that returns an array of all of the listeners for a particular list. - Extend the
EventSetDescriptorclass to cover the newget<ListenerType>s()method. So that BeanInfos returned for classes by Introspection can recognize the new event pattern.
The rest of the document describes the changes to the JavaBeans Specification to support the new get<ListenerType>s() extension.
The additional methods and classes presented in this amendment have been implemented for Java 2 Standard Edition (J2SE) v 1.4. JavaBeans which have been written that extend Beans in the java.awt or javax.swing packages will automatically pick up the implementation of this amendment for existing listeners when J2SE 1.4 is used . If the extended Bean defines additional listeners, then the extended Bean should implement the appropriate






