|
101, Part III: Introduction | Page 1 | Page 2 | Page 3 | Page 4 Bound Properties As you recall, JavaBeans use bound properties. This section adds support for bound properties to the evolving NervousText example. The example code shows how to fire property events, how components maintain listeners wishing to be notified of change events, and how to respond to property change event notices. In this section you add bound property support to NervousText04. An object that supports bound properties can notify other objects when certain properties change. Such a notification is called a property change event. Each time a bound property changes, it notifies registered listener objects by calling specified property change handler methods defined for the listener. These methods are called with the new and old values of the property, as well as name of the property that has changed. Step 1. Add an import statement to support property change events For a Bean to generate or receive property change events, you must add the following import statement to the top of the file.
import java.beans.*; The package and import statements at the top of the file should be as follows: Step 2. Add code to track and notify interested property change listeners Add the following code to maintain a list of listeners potentially wanting to be notified of bound property changes:
The last statement declares an instance variable, Step 3. Add code to fire property change events
Modify the property setter method,
To save the old property, add the following line to the end of the String oldstring = s;
Recall that public class NervousText04 extends Panel implements Runnable, MouseListener { ... String s = null; ...
The When you have both the old and new values saved in local variables, add the following line to the end of the
support.firePropertyChange("text", oldstring, newstring);
The full method now looks like this: Step 4. Define event handler methods for property change events
Define a handler method,
Add the code for
private PropertyChangeSupport support = new PropertyChangeSupport(this); NervousText04 is now both a source for property change events and a handler for property change events. We haven't made it a listener because the BeanBox automatically generates a listener class as an adapter. The BeanBox creates an adapter when you connect an object that generates a property change to a target NervousText04 object when building a BeanBox application.
The
Define an additional method called
Add the method definition for Step 5. Build the JAR file and install it in the BeanBox javac -d . NervousText04.java Create the manifest file in a text editor: Name: sun/beanbox/beans/NervousText04.class Java-Bean: True Create the JAR file: jar cfm NervousText04.jar manifest.tmp sun/beanbox/beans/NervousText04.class
Remove the temporary manifest and install the JAR in the BeanBox jars directory (substituting your BDK installation directory for rm manifest.tmp cp -p NervousText04.jar BDK_HOME/beans/jars Step 6. Test NervousText04 in the BeanBox
The changes to the first NervousText04 are effected by pressing OurButton.
Program Source Code A makefile for this lesson automates source code compilation, JAR file construction, and copying JAR files to the appropriate BeanBox directory. You have to edit several of the variables in the makefile to indicate the location of your JDK 1.1 and the BDK installation directories.
You may want to look at the final source file for NervousText04.java, to verify the changes you have made to the original
Introduction | Page 1 | Page 2 | Page 3 | Page 4 | ||||
|
| ||||||||||||