Java Reference
In-Depth Information
kinds of events are available for use by JavaBeans properties: A
PropertyChange
event can be fired when a JavaBeans
property is changed; a
VetoableChange
event can also be fired when a JavaBeans property is changed; and if the
listener throws a
PropertyVetoException
, the property change should not take effect. A property whose setter fires
PropertyChange
events is called a
bound property
. A property whose setter fires
VetoableChange
events is called a
constrained property
. Helper classes
PropertyChangeSupport
and
VetoableChangeSupport
allow bound properties
and constrained properties to be easily defined in JavaBean classes.
Listing 4-16 defines a JavaBean
Person
with three properties:
name
,
address
, and
phoneNumber
. The
address
property is a bound property, and the
phoneNumber
property is a constrained property.
Listing 4-16.
Person.java
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
public class Person {
private PropertyChangeSupport propertyChangeSupport;
private VetoableChangeSupport vetoableChangeSupport;
private String name;
private String address;
private String phoneNumber;
public Person() {
propertyChangeSupport = new PropertyChangeSupport(this);
vetoableChangeSupport = new VetoableChangeSupport(this);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
String oldAddress = this.address;
this.address = address;
propertyChangeSupport.firePropertyChange("address", oldAddress, this.address);
}
public String getPhoneNumber() {
return phoneNumber;
}
Search WWH ::
Custom Search