Java Reference
In-Depth Information
5.
Before a constrained property is changed, have the VetoableChange-
Support object notify all listeners by invoking one of the fireVetoable-
Change() methods of the VetoableChangeSupport class. If no one vetoes
the change, then the bean can go ahead and make the requested change.
If one listener vetoes the change, the change should not be made to the
property and the bean should notify all listeners of this.
To veto a change, a listener throws a PropertyVetoException. If this excep-
tion occurs, the VetoableChangeSupport handles it for the bean and also noti-
fies all listeners that the new value is not being used; instead, the bean is
reverting to the old value. As a bean developer, your set method can simply
declare the PropertyVetoException.
When a constrained property is changed (within a set method of the bean
class), the bean needs to invoke one of the following fireVetoableChange()
methods found in the VetoableChangeSupport class:
public void fireVetoableChange(String propertyName, Object oldValue,
Object newValue). Use this method for properties of any data type
because the old and new values are of type Object.
public void fireVetoableChange(String propertyName, int oldValue, int
newValue). Use this method for properties of type int.
public void fireVetoableChange(String propertyName, boolean old-
Value, boolean newValue).
Use this method for properties of type
boolean.
The following Customer class is a modification of the earlier Customer class,
except that the accountNumber is now both a bound property and a con-
strained property. Study this Customer class and notice that it follows the
steps for creating a constrained property.
package video.store;
import java.beans.*;
public class Customer implements java.io.Serializable
{
private String name;
private int number;
private PropertyChangeSupport pcs;
private VetoableChangeSupport vcs;
public Customer()
{
name = “”;
Search WWH ::




Custom Search