Developing Java Beans 4.5 Handling Events for Specific Properties If a Bean component supports bound properties, it is required to support the binding mechanism described earlier. This means that it must provide an addPropertyChangeListener() method for any client object that wants to receive bound property changes via the PropertyChangeListenerinterface. The same holds true for constrained properties. In that case, the Bean must provide an addVetoableChangeListener() method for any client object that wants to receive VetoableChangeEvent notifications via the VetoableChangeListener interface. But the Bean also has the option of providing specific registration methods for listeners to register for property change and vetoable change notifications for specific properties. There is no requirement to support these mechanisms, but they may be useful under some circumstances. There is a design pattern for registering and unregistering event listeners for changes to specific bound properties. The method signatures are as follows: public void addListener(PropertyChangeListener p); public void removeListener(PropertyChangeListener p); There is also a design pattern for registering and unregistering event listeners for vetoable change events for specific constrained properties. The method signatures are as follows: public void addListener(VetoableChangeListener p); public void removeListener(VetoableChangeListener p); This is convenient if a source object supports many properties and you are only interested in one. It eliminates unwanted PropertyChangeEvent and VetoableChangeEvent notifications. If you’re interested in changes for multiple properties, however, this technique probably doesn’t add any value. All of the property changes will still be directed to your one and only implementation of the propertyChange() or vetoableChange() methods. If you wanted to direct various property change events to different methods, you could combine these patterns with the adapter technique described in Chapter 3. 4.6 A java.awt Example The components provided by the java.awt package expose properties using the techniques just described. These user interface elements can be manipulated in the same way as any other object that conforms to the JavaBeans architecture. So let’s create an applet that illustrates the concepts of bound and constrained properties. Our applet will have a data member named primaryLabel which is an instance of class NumberLabel. A NumberLabel is a subclass of java.awt.Label that has a bound and constrained property named Value. The Value property is of type int, and the label always displays the contents of this property. We also have another instance of the NumberLabel class, named mirrorLabel, that will bind itself to the Value property of primaryLabel, and will keep its display consistent with it. There is one button to decrement the Value property of primaryLabel, and one to increment it; these button instances will be named decButton and incButton, respectively. Lastly, we will create an instance of class Constrainer, named cnstr, that constrains the Value property of primaryLabel to be between 10 and 20. A diagram of the interactions that will take place between the objects is shown in Figure 4.3. Figure 4.3. Constraining a property with a VetoableChangeEvent page 64
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services
This entry was posted
on Saturday, September 30th, 2006 at 3:31 am and is filed under java.
You can follow any responses to this entry through the RSS 2.0 feed.
Responses are currently closed, but you can trackback from your own site.