Listening for a Property Change Event: A property change event is fired when a bound property is changed.
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class Main {
public static void main(String[] argv) throws Exception {
MyBean bean = new MyBean();
bean.addPropertyChangeListener(new MyPropertyChangeListener());
}
}
class MyPropertyChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
Object oldValue = evt.getOldValue();
Object newValue = evt.getNewValue();
}
}
class MyBean {
PropertyChangeSupport pceListeners = new PropertyChangeSupport(this);
int myProperty;
public int getMyProperty() {
return myProperty;
}
public void setMyProperty(int newValue) {
int oldValue = myProperty;
myProperty = newValue;
pceListeners.firePropertyChange("myProperty", new Integer(oldValue),
new Integer(newValue));
}
public synchronized void addPropertyChangeListener(
PropertyChangeListener listener) {
pceListeners.addPropertyChangeListener(listener);
}
public synchronized void removePropertyChangeListener(
PropertyChangeListener listener) {
pceListeners.removePropertyChangeListener(listener);
}
}
Related examples in the same category