If a Bean component supports bound
properties, it is required to support the binding mechanism described
earlier. This means that it must provide an
addPropertyChangeListener()
method for any client object that wants to
receive bound property changes via the
PropertyChangeListener
interface. The same holds
true for constrained properties. In that case, the Bean must provide
an
addVetoableChangeListener()
method for any client object that wants to
receive VetoableChangeEvent
notifications via the
VetoableChangeListener
interface. But the Bean
also has the option of providing specific registration methods for
listeners to register for property change and vetoable change
notifications for specific properties. There is no requirement to
support these mechanisms, but they may be useful under some
circumstances.
There is a design pattern for registering and unregistering event listeners for changes to specific bound properties. The method signatures are as follows:
public void add<PropertyName>Listener(PropertyChangeListener p); public void remove<PropertyName>Listener(PropertyChangeListener p);
There is also a design pattern for registering and unregistering event listeners for vetoable change events for specific constrained properties. The method signatures are as follows:
public void add<PropertyName>Listener(VetoableChangeListener p); public void remove<PropertyName>Listener(VetoableChangeListener p);
This is convenient if a source object supports many properties and
you are only interested in one. It eliminates unwanted
PropertyChangeEvent
and
VetoableChangeEvent
notifications. If you’re
interested in changes for multiple properties, however, this
technique probably doesn’t add any value. All of the property
changes will still be directed to your one and only implementation of
the propertyChange()
or
vetoableChange()
methods. If you wanted to direct
various property change events to different methods, you could
combine these patterns with the adapter technique described in Chapter 3.
Get Developing Java Beans now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.