The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.beans  [12 examples]

e3. Implementing a Constrained Property

A constrained property fires a PropertyChangeEvent whenever its value is about to be changed. Any listener can veto the event, thereby preventing the change. This example bean implements a single constrained integer property called myProperty.
    
int myProperty;
    public int getMyProperty() {
        return myProperty;
    }
    public void setMyProperty(int newValue) throws PropertyVetoException {
        try {
            vceListeners.fireVetoableChange(
                "myProperty", new Integer(myProperty), new Integer(newValue));
            myProperty = newValue;
        } catch (PropertyVetoException e) {
            throw e;
        }
    }
    
    // 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);
    }

 Related Examples
e1. The Quintessential Bean
e2. Implementing a Bound Property
e4. Instantiating a Bean
e5. Listing the Property Names of a Bean
e6. Getting and Setting a Property of a Bean

See also: Events    Serialization   


© 2002 Addison-Wesley.