A bound property informs interested components that a change has taken place; a constrained property gives listeners the opportunity to reject, or veto, the change. You can use constrained properties to prevent components from changing their abilities; for example, you can make sure that a timer component will never have its delay changed to an unacceptably short (or long) value. For example, if I wanted to allow other components to veto changes in the color of a SimpleBean, I could implement the following process.
public class SimpleBean extends Canvas
{
// New field for vetoable changes
private VetoableChangeSupport vetoes =
new VetoableChangeSupport(this);
private PropertyChangeSupport changes =
new PropertyChangeSupport(this);
public Color getColor()
{
return getBackground();
}
public void setColor(Color new_color)
throws PropertyVetoException
{
Color old_color = getBackground();
// Inform listeners of the pending change.
// The exception thrown will be sent to whoever
// called setColor.
vetoes.fireVetoableChange("New color", old_color,
new_color);
// Now make the change
setBackground(new_color);
// Send change event to listeners
changes.firePropertyChange("New color", old_color,
new_color);
}
// Methods to manage veto listeners
public void addVetoableChangeListener(
VetoableChangeListener l)
{
vetos.addVetoableChangeListener(l);
}
public void removeVetoableChangeListener(
VetoableChangeListener l)
{
vetos.removeVetoableChangeListener(l);
}