Constrained Properties
To this point we’ve assumed that all the property changes that take place are acceptable. Often this will not be the case; it’s possible that an attempt to set a property to a given value will be unacceptable and therefore rejected. The simple case is when the object that owns the property wants to reject the change, but there may be times when another object wants a chance to voice its disapproval. Properties that go through this approval process are known as constrained properties.
The
design pattern for setting and
getting constrained properties is similar to the design pattern for
properties that are not constrained. The difference is that the set
method declares that it throws the exception
java.beans.PropertyVetoException. The method
signatures look as follows:
public <PropertyType> get<PropertyName>();
public void set<PropertyName>(<PropertyType> value)
throws java.beans.PropertyVetoException;Binding to Non-Specific Constrained Properties
Those objects that want to participate in
the approval process for a change to a constrained property must
implement the
java.beans.VetoableChangeListener
interface. This interface contains a
method called
vetoableChange()
that takes a single parameter of type
PropertyChangeEvent. This method may throw the
java.beans.PropertyVetoException
if it wants to reject the change.
Objects that support constrained properties must provide methods for
registering and unregistering
VetoableChangeListener objects. The methods ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access