Javaでプロパティ変更事件(PropertyChangeEvent)の処理方法
参考コード:
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
/**
* Javaでプロパティ変更事件(PropertyChangeEvent)の処理
*
*/
public class PropertyChangeTest {
private PropertyChangeSupport accessibleChangeSupport;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PropertyChangeTest test=new PropertyChangeTest();
test.addPropertyChangeListener(new PropertyChangeListener(){
@Override
public void propertyChange(PropertyChangeEvent evt) {
// TODO Auto-generated method stub
}
});
}
int a=1;
public int getA() {
return a;
}
public void setA(int a) {
int oldValue=this.a;
this.a = a;
this.accessibleChangeSupport.firePropertyChange(“a", oldValue, this.a);
}
/**
* 追加 Adds a PropertyChangeListener to the listener list.
* The listener is registered for all Accessible properties and will
* be called when those properties change.
* @param listener The PropertyChangeListener to be added
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
if (accessibleChangeSupport == null) {
accessibleChangeSupport = new PropertyChangeSupport(this);
}
accessibleChangeSupport.addPropertyChangeListener(listener);
}
/**
* 削除 Removes a PropertyChangeListener from the listener list.
* This removes a PropertyChangeListener that was registered
* for all properties.
*
* @param listener The PropertyChangeListener to be removed
*/
public void removePropertyChangeListener(PropertyChangeListener listener) {
if (accessibleChangeSupport != null) {
accessibleChangeSupport.removePropertyChangeListener(listener);
}
}
}