Bean Internals  «Prev  Next»
Lesson 11Bound properties
ObjectiveLearn about Bound Properties.

JavaBeans Bound Property Notification

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

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
  1. the bean's container or
  2. 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.

Developing Java Beans