Java Reference
In-Depth Information
5.
Whenever a bound property changes, have the PropertyChangeSupport
object notify all listeners by invoking one of the firePropertyChange()
methods of the PropertyChangeSupport class.
The last step is the critical step. The first four steps are essentially mainte-
nance steps to register and keep track of listeners. When a property actually
changes, the set method of the property is invoked. Within the set method, you
need to notify all listeners bound to that property by invoking one of the fol-
lowing methods:
public void firePropertyChange(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 firePropertyChange(String propertyName, int oldValue, int
newValue). Use this method for properties of type int.
public void firePropertyChange(String propertyName, boolean old-
Value, boolean newValue).
Use this method for properties of type
boolean.
To demonstrate bound properties, examine the following bean class named
Customer, which has two properties, both of them bindable to other bean
properties. Study the class and notice how it follows the previously discussed
steps for creating bound properties.
package video.store;
import java.beans.*;
public class Customer implements java.io.Serializable
{
private String name;
private int number;
private PropertyChangeSupport pcs;
public Customer()
{
name = “”;
pcs = new PropertyChangeSupport(this);
}
public void addPropertyChangeListener(PropertyChangeListener p)
{
pcs.addPropertyChangeListener(p);
}
public void removePropertyChangeListener(PropertyChangeListener p)
{
pcs.removePropertyChangeListener(p);
}
Search WWH ::




Custom Search