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); }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.