Java Reference
In-Depth Information
Figure 19.19
The JSlider changes position depending on the value of the Customer bean's
accountNumber property.
Figure 19.19 shows what happens to the JSlider bean when the account-
Number property is changed.
Constrained Properties
A
constrained property
is a bean property whose changes are monitored by one
or more other beans. These other beans validate any changes to the con-
strained property and can veto a change if they do not find it appropriate. If a
change is vetoed, the bean should not make the change and should notify all
listeners of this decision.
As with bound properties, because constrained properties are a common
occurrence in JavaBeans, the JavaBeans API contains the java.beans.Vetoable-
ChangeSupport class to handle the process of registering and notifying listen-
ers of a constrained property. Here are the steps involved in creating a
constrained property:
1.
Add a field of type VetoableChangeSupport.
2.
Add the method addVetoableChangeListener() to the bean class, which
takes in a VetoableChangeListener object. Within this method, you add
the given listener object to the VetoableChangeSupport object.
3.
Add the method removeVetoableChangeListener() to the bean class,
which also takes in a VetoableChangeListener object. Using the
VetoableChangeSupport object, remove the given listener.
4.
Add the method getVetoableChangeListeners(), which returns an array
containing all listeners currently constrained to properties of this bean.