Java - Allgemein

Beans, gebunden, vetoable

Ein einfaches Bean:

public class MyBean {
   int myProperty;
 
   public int getMyProperty() {
      return myProperty;
   }
 
   public void setMyProperty(int newValue) {
      myProperty = newValue;
   }
}



Ein Bean mit PropertyChangeSupport:

public class MyBean {
   PropertyChangeSupport  pcs = new PropertyChangeSupport(this);
   int                    myProperty;
 
   public int getMyProperty() {
      return myProperty;
   }
 
   public void setMyProperty(int newValue) {
      int oldValue = myProperty;
      myProperty = newValue;
      pcs.firePropertyChange("myProperty", new Integer(oldValue), new Integer(newValue));
   }
 
   public void addPropertyChangeListener(PropertyChangeListener listener) {
      pcs.addPropertyChangeListener(listener);
   }
 
   public void removePropertyChangeListener(PropertyChangeListener listener) {
      pcs.removePropertyChangeListener(listener);
   }
}


… und mit VetoableChangeSupport:

public class MyBean {
   VetoableChangeSupport  vcs = new VetoableChangeSupport(this);
   int                    myProperty;
 
   public int getMyProperty() {
      return myProperty;
   }
 
   public void setMyProperty(int newValue) {
      try {
         vcs.fireVetoableChange("myProperty", new Integer(myProperty), new Integer(newValue));
         myProperty = newValue;
      } catch (PropertyVetoException e) {
         throw e;
      }
   }
 
   public synchronized void addVetoableChangeListener(VetoableChangeListener listener) {
      vcs.addVetoableChangeListener(listener);
   }
 
   public synchronized void removeVetoableChangeListener(VetoableChangeListener listener) {
      vceListeners.removeVetoableChangeListener(listener);
   }
}



Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007