Developing Java Beans reject an unacceptable change to

Developing Java Beans The NumberLabel class extends java.awt.Label, a class for creating a static text field. NumberLabel implements the java.beans.PropertyChangeListener interface because we will use one instance of the NumberLabel class to monitor the Value property of another. Since NumberLabel supports a property named Value that is both bound and constrained, we use an instance of java.beans.PropertyChangeSupport as well as an instance of VetoableChangeSupport. The implementation of the methods addPropertyChangeListener(), removePropertyChangeListener(), addVetoableChangeListener(), and removeVetoable- ChangeListener() all defer to their respective support objects. Since the Value property is read/write, we implement a getValue() method as well as a setValue() method. The latter declares that it can throw the java.beans.PropertyVetoException because the Value property is constrained as well as bound. The setValue() method first instructs the support object to fire a VetoableChangeEvent for the Value property by calling the fireVetoableChange() method. If the event is not vetoed, SetValue() stores the new value, updates the label’s text, and repaints. Finally, the other support object is instructed to fire a PropertyChangeEvent by invoking the firePropertyChange() method. The NumberLabel class also implements the propertyChange() method to handle property changes from other objects. In this example, we know that a NumberLabel listens for property changes only from the other instance of NumberLabel. We can therefore take a shortcut and simply have the NumberLabel set its own Value property based on the new value of the PropertyChangeEvent that it received. This is guaranteed to keep our two labels in sync. Of course, we first check to make sure that it was the Value property that changed, and that we haven’t received a notification about some other property. Now let’s take a look at the code for the Constrainer class: class Constrainer implements VetoableChangeListener { // handle the vetoable change event public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException { // we constrain the value to between 10 and 20 Integer val = (Integer)evt.getNewValue(); if (val.intValue() < 10 || val.intValue() > 20) { throw new PropertyVetoException(”Bad Value”, evt); } } } The Constrainer class vetoes any property changes that attempt to change the Value property of a NumberLabel object to a value below 10 or above 20. The vetoableChange() method examines the new value and rejects it if it does not meet the criteria. Now we can create an applet that ties all this together. The applet uses an instance of the class BeansBook.util.GenericButtonAdapter to route action events that occur when the two buttons are pressed. Here’s what the code looks like: public class ExampleApplet4 extends Applet { // the button adapter for handling button action events protected GenericButtonAdapter adapter; page 67
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services

Comments are closed.