Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 
Training Index

Java Beans Tutorial, Part 4
Bound Properties and Property Change Events

By Greg Voss, JavaSoft

[Tutorial Contents]

Now you'll be adding support for bound properties to NervousText04. An object that supports bound properties can notify other objects when certain property values 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 which has changed.

Step 1. Add import Statement to Support Property Change Events

To enable 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:
package sun.beanbox.beans;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.*;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.beans.*;

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 changes for the text property.

public void addPropertyChangeListener(
              PropertyChangeListener l) {
  support.addPropertyChangeListener(l);
}

public void removePropertyChangeListener(
              PropertyChangeListener l) {
  support.removePropertyChangeListener(l);
}

private PropertyChangeSupport support = 
          new PropertyChangeSupport(this);
The last statement in this listing declares an instance variable, support, for NervousText04 objects. This variable holds a collection of listener objects that will be notified about property changes to the text property. The two supporting methods, addPropertyChangeListener and removePropertyChangeListener provide a public interface allowing interested listeners to register themselves with NervousText04 objects.

Step 3. Add Code to Fire text Property Change Events

Now 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 setText method in NervousText04.

  String oldstring = s;

Recall that s is the value of the text property accessed by the getText and setText getter and setter methods for the text property. The instance variable s is declared at the top of the class

public class NervousText04 extends Panel 
  implements Runnable, MouseListener {
  
  ...
  String s = null;
  ...
Now that you have both old a new values saved in local variables Add the following line to the end of the setText method as well.
  support.firePropertyChange(
    "text", oldstring, newstring);
The full method now looks like this:
  public void setText(String newstring){
    String oldstring = s; 
    s=new String(newstring);
    separated= new char[s.length()];
    s.getChars(0,s.length(),separated,0);
    support.firePropertyChange(
      "text", oldstring, newstring);
  }

Step 4. Define Event Handler Methods for Property Change Events

Now define a handler method that can potentially be called by a listener object to report that a text property change has occurred.

  public void reportChange(PropertyChangeEvent evt) {
    System.err.println(
      "ENTER---> NervousText04 reportChange");

    String oldValue = evt.getPropertyName() + 
      " := " + evt.getOldValue();
    String newValue = evt.getPropertyName() + 
      " := " + evt.getNewValue();

    System.err.println("New value: " + oldValue);
    System.err.println("Old value: " + newValue);
    System.err.println(
      "EXIT----> NervousText04 reportChange");
  }
Add the above code for reportChange at the end of the class definition for NervousText04 immediately after the declaration for the support variable you added earlier:
  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 reportChange method does not actually cause a change to take place in the target object. To do that, define an additional method called makeChange as follows:

  public void makeChange(PropertyChangeEvent evt) {
    System.err.println(
      "ENTER---> NervousText04 makeChange");

    String oldValue = (String)evt.getOldValue(); 
      // (String) cast required since 
    String newValue = (String)evt.getNewValue(); 
      // evt.getOldValue() returns an Object
    setText("*" + newValue + "*");

    System.err.println("New value: " + oldValue);
    System.err.println("Old value: " + newValue);
    System.err.println(
      "EXIT----> NervousText04 makeChange");
  }

Add the method definition for makeChange immediately after the code for reportChange. The actual change in the target NervousText object occurs when the setter method, setText is called in the makeChange event handler method..

Step 4. Build the JAR and Install in BeanBox

Compile NervousText04.java:

  javac -d . NervousText04.java

Create the mainfest:

  echo "Manifest-Version: 1.0" > manifest.tmp
  echo "" >>
manifest.tmp
  echo "Name: sun/beanbox/beans/NervousText04.class" >>
manifest.tmp
  echo "Java-Bean: True" >>
manifest.tmp
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

NO: Use the exiting app, and modify it.

Start up the BeanBox. Add one OurButton, and two NervousText04 beans. You're going to bind changes in the first NervousText04 bean to the second. The changes to the first NervousText04 will be effected by pressing the OurButton. Change the label for OurButton to "change direction." As before, you're going to make an action event in OurButton cause the text to be reversed in the first NervousText04 bean.


------------------------------------------------
diffs
/matisse/usr/home/sparky_gvoss/work97/06/test/
beans-example-code/NervousText/1.1/dodiffs.out /matisse/usr/home/sparky_gvoss/work97/06/test/
beans-example-code/NervousText/1.1/dodiffs.out

-------------------------------------------------

Program Source Code

The makefile for this lesson automates source code compilation, JAR file construction, and copying of 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 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.


[ This page was updated: 5-Nov-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.