It is sometimes useful for an application to be able to know when a Bean property changes. JavaBeans supports this ability through bound properties, which are properties that provide a notification service upon being changed. Bound properties are registered with an outside party (an application, applet, or Bean) that is interested in knowing about changes in the property. Whenever the value of a bound property changes, the interested party is notified. These properties are called bound properties because they are effectively bound to an outside party via changes in their value.
Outside parties can be applications, applets, or other Beans, and are commonly referred to as listeners.
Bound Properties and Events, event sources, and event listeners
The concept of a listener comes from the Java 1.1 delegation event model, which is based on
events,
event sources, and
event listeners.
An event is a notification sent to an interested party whenever some action takes place. In the case of bound properties, the action is the changing of the property. Also, the event source is the Bean whose bound property changed, while the event listener is the interested party receiving the notification.
java.util
The Java package java.util provides basic support for the event model. It provides a base class for event objects, as well as a base interface for event listeners. All of the classes and interfaces in the Java class libraries that use the event model make use of these classes. When you are developing Beans, or any other Java class that leverages the event model, you will be working directly with these classes and interfaces or their subclasses.
Bound Properties
In the previous example, property changes can take place when one of the setStocks() methods are invoked.
This results in the state of the WatchList being changed. What would happen if two objects were using the WatchList and one of them changed the Stocks property? It is possible that users of the WatchList will want to know if its properties are changed.
A Bean component can provide change notifications for its properties. These notifications take the form of events, and they conform to the event model described in the earlier lessons. Properties that support change notifications are known as bound properties[1], because other objects can bind themselves to changes in their value.
Bound and Constrained Properties:
JavaBeans provide support for 1) bound and 2) constrained properties.
These are advanced features that are not required for simple beans or for initial beans programming.
Bound properties
Sometimes when a bean property changes then either
the bean's container or
some other bean may wish to be notified of the change.
A component can choose to provide a change notification service for some or all of its properties. Such properties are commonly known as bound properties, as they allow other components to bind special behaviour to property changes. The PropertyChangeListener event listener interface is used to report updates to simple bound
properties. If a bean supports bound properties then it should support a normal pair of multicast event listener registration methods for PropertyChangeListeners:
public void addPropertyChangeListener(PropertyChangeListener x);
public void removePropertyChangeListener(PropertyChangeListener x);
When a property change occurs on a bound property the bean should call the PropertyChange-Listener.propertyChange method on any registered listeners, passing a PropertyChangeEvent .
In the next lesson, the mechanics of bound properties will be discussed.
[1]Bound property: a property that provides notifications to an interested party based on changes in its value.