|
By Greg Voss, JavaSoft If you're building Beans, you want to know about bound properties. An object that supports bound properties can notify other objects when certain property values change. Such a notification is called a property change event. This section starts by adding 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 will be adding bound property support to NervousText04. An object that supports bound properties can notify other objects when certain properties change. 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 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 text property changes:
The last statement in this listing declares an instance variable,
Step 3. Add Code to Fire
Modify the text property setter method to fire a property change event whenever
it is called. To do this, you need to save copies of both the old and new
values of the property being changed. To save the old property, add the
following line to the end of the String oldstring = s;
Recall that "s" is the value of the text property accessed by the
Now that you have both old a 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 that can potentially be called by a listener object to report that a text property has changed.
Add the above code for
private PropertyChangeSupport support =
new PropertyChangeSupport(this);
Now NervousText04 is both a source for property change events and a handler for property change events. We haven't made it a listener. That's because the BeanBox will automatically generate a listener class as an adapter. An adapter is created when you connect any object that generates a property change to a NervousText04 object as a target when building a BeanBox application.
The
Add the method definition for Step 4. Build the JAR and Install it in the BeanBox Compile NervousText04.java: javac -d . NervousText04.java Create the mainfest:
The resulting temporary manifest file should look like this: Manifest-Version: 1.0 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 BDK_HOME): rm manifest.tmp cp -p NervousText04.jar BDK_HOME/beans/jars Step 5. Test NervousText04 in BeanBox
Start up the BeanBox. Add one 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'll 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 NervousText Bean Version 04 to verify the changes you have made to the original NervousText.java file. | |||||||||||||
|
| ||||||||||||