Implementing a Constrained Property - Java Object Oriented Design

Java examples for Object Oriented Design:Java Bean

Description

Implementing a Constrained Property

Demo Code


import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;

public class Main {
  int myProperty;

  public int getMyProperty() {
    return myProperty;
  }//from w w w. j av a  2 s.  c om

  // Create the listener list.
  VetoableChangeSupport vceListeners = new VetoableChangeSupport(this);

  // The listener list wrapper methods.
  public synchronized void addVetoableChangeListener(
      VetoableChangeListener listener) {
    vceListeners.addVetoableChangeListener(listener);
  }

  public synchronized void removeVetoableChangeListener(
      VetoableChangeListener listener) {
    vceListeners.removeVetoableChangeListener(listener);
  }

  public void setMyProperty(int newValue) throws PropertyVetoException {
    try {
      vceListeners.fireVetoableChange("myProperty", new Integer(myProperty),
          new Integer(newValue));
      myProperty = newValue;
    } catch (PropertyVetoException e) {
      throw e;
    }
  }

}

Related Tutorials