Bean Internals  «Prev  Next»
Lesson 14Constrained properties
ObjectiveLearn about constrained properties.

JavaBeans Constrained Properties

JavaBeans support

constrained properties, which are properties that enable an outside party to validate a change in them before accepting the change.
When you attempt to change the value of a constrained property, the value is first checked with an outside validation source that accepts or rejects the change. Property change rejections come in the form of PropertyVetoException exceptions, which are handled by the Bean containing the property. When a rejection exception occurs, the Bean is responsible for reverting the property back to its prior value and issuing a new property change notification for the reversion.

JavaBean Constrained Property

Constrained property: a property that enables an interested party to perform a validation on a new property value before accepting the modification. In the next lesson, how constrained properties work will be discussed.

A bean property is constrained if the bean supports the VetoableChangeListener(in the API reference documentation) and PropertyChangeEvent(in the API reference documentation) classes, and if the set method for this property throws a PropertyVetoException(in the API reference documentation). Constrained properties are more complicated than bound properties because they also support property change listeners which happen to be vetoers.
The following operations in the setXXX method for the constrained property must be implemented in this order:
  1. Save the old value in case the change is vetoed.
  2. Notify listeners of the new proposed value, allowing them to veto the change.
  3. If no listener vetoes the change (no exception is thrown), set the property to the new value.
The accessor methods for a constrained property are defined in the same way as those for simple properties, with the addition that the setXXX method throws a PropertyVetoException exception. The syntax is as follows:
public void setPropertyName(PropertyType pt)
throws PropertyVetoException {code}