先看PropertyChangeSupport类的官方文档解释:
This is a utility class that can be used by beans that support bound properties. You can use an instance of this class as a member field of your bean and delegate various work to it.
void addPropertyChangeListener(PropertyChangeListener listener);
void removePropertyChangerListener(PropertyChangerListener listener);
|
private PropertyChangeSupport listeners = new PropertyChangeSupport(this);
|
public void addPropertyChangeListener(PropertyChangeListener listener) {
listeners.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
listeners.removePropertyChangeListener(listener);
}
|
listeners.firePropertyChange("ourString", oldString, newString);
|
public void propertyChange(PropertyChangeEvent e)
|
e.getOldValue();
e.getNewValue();
|
void fireIndexedPropertyChange(String PropertyName,int index,Object oldValue,Object newValue)
|
4.实例代码
publicclass SomeBean {
private String property;
private PropertyChangeSupport changeSupport;
publicvoid setProperty(String newValue) {
String oldValue = property;
property = newValue;
changeSupport.firePropertyChange("property", oldValue, newValue);
}
publicvoid addPropertyChangeListener(PropertyChangeListener l) {
changeSupport.add(l);
}
publicvoid removePropertyChangeListener(PropertyChangeListener l) {
changeSupport.remove(l);
}
}
参考文章:
http://www.javalobby.org/java/forums/t19476.htmlhttp://feelingsea.blog.hexun.com/7293978_d.html