Java Reference
In-Depth Information
Vetoing an Event
To veto a constrained property, a listener must implement the java.beans.
VetoableChangeListener interface. This interface contains one method:
public void vetoableChange(PropertyChangeEvent e)
throws PropertyVetoException
This method is invoked on registered listeners before the property is
changed. The PropertyChangeEvent parameter contains the name of the prop-
erty, its old value, and the new value being requested. To demonstrate a lis-
tener of constrained properties, the following StoreOwner bean is a listener of
changes made to the accountNumber property of Customer beans. Study the
class and try to determine when changes are vetoed.
package video.store;
import java.beans.*;
public class StoreOwner implements VetoableChangeListener,
java.io.Serializable
{
public void vetoableChange(PropertyChangeEvent e) throws
PropertyVetoException
{
if(e.getPropertyName().equals(“accountNumber”))
{
Integer temp = (Integer) e.getNewValue();
int newValue = temp.intValue();
if(newValue <= 0 || newValue > 100)
{
System.out.println(“Vetoing change!”);
throw new PropertyVetoException(“Out of range”, e);
}
else
{
System.out.println(newValue + “ is OK with me!”);
}
}
}
}
Note the following about the StoreOwner bean:
I will point this out one last time: The bean class implements Serializ-
able and contains a no-argument constructor (which in this case is the
default constructor).
■■
The getPropertyName() method of the PropertyChangeEvent object is
used to make sure that the listener is validating the correct property.
■■
Search WWH ::




Custom Search