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

JavaBeansTM Short Course
Help: From Kiva Han to Central Perk

By MageLang Institute

[Tutorial Contents]
[Behavior] [API Docs] [Exercise] [Solution]

Help is available for each task, or you can go straight to the solution source code.

Task 1

The message and the moving rate are two properties for CentralPerk (properties like font and background/foreground color are inherited through Canvas). There are currently setter/getter routines for the message property. Add routines for the MovingRate property.
Their method signatures need to be:
public void setMovingRate (int rate);
public int getMovingRate ();

Task 2

In order to make our properties bound, we need to maintain a PropertyChangeSupport list and add/remove listeners to it. Also, the property set routines need to be modified to fire property changes to the listeners when the changes happen. Be sure to update both setMessage and setMovingRate.
The listener list should be private:
private PropertyChangeSupport changes = 
  new PropertyChangeSupport (this);
The adding and removing of listeners routines should be public:
public void addPropertyChangeListener (
    PropertyChangeListener p) {
  changes.addPropertyChangeListener (p);
}
public void removePropertyChangeListener (
    PropertyChangeListener p) {
  changes.removePropertyChangeListener (p);
}
In the set routines, be sure to call changes.firePropertyChange.

Task 3

Whenever a message starts scrolling in from the right, we want to generate a PerkEvent. In order for this to happen, we need to define the event, and give it a read-only message property.
Bean events need to subclass java.util.EventObject. Remember to create a getMessage method for the message property.

Task 4

Next, we need to create a PerkListener and give it a method for us to call when the PerkEvent happens. Lets call it startedPerking.
Listeners need to implement java.util.EventListener. Its startedPerking routine needs to accept a PerkEvent parameter.

Task 5

Back in CentralPerk, we need to maintain a PerkListener list and add/remove listeners to it. Also, when the event happens, we need to notify the listeners.
  • An event listener list is usually maintained in a Vector, although any collection will do.
  • If you used a Vector, the add listener routine would call addElement.
  • If you used a Vector, the remove listener routine would call removeElement.
  • The notification routine is usually protected so subclasses can override. It needs to create a PerkEvent, copy (clone) the listener list (so it won't change while firing), and notify each of the listeners via the listener's startedPerking method.
  • Finally, in paint, call our notification method when the message restarts scrolling from right.

Task 6

Finally, we need to check if everything is Serializable. Flag anything that isn't as transient
All the basic data structures and datatypes are Serializable, so you don't have to worry about things like String, Vector, and PropertyChangeSupport. However, Thread is not. Mark Thread as a transient variable.

Task 7

Because of the nature of our transient variable, we need to override the default serialization reading routine readObject to initialize the variable. The writing routine writeObject doesn't have to do anything special, but because of the requirements of serialization is required to be present. Add a readObject and writeObject routine to CentralPerk.
  • The write routine should have a method signature of:
    private void writeObject(ObjectOutputStream s)
    throws IOException
    It only needs to call s.defaultWriteObject()
  • The read routine should have a method signature of:
    private void readObject(ObjectInputStream s)
    throws ClassNotFoundException, IOException
    It needs to call s.defaultReadObject() and initMoving.

Task 8

Okay, now that we've finished our Bean, we can try it out. Use the CentralPerkTester applet and loader to try it out.
There is a label at the top of the applet that listens for the message property to change. Whenever the PerkEvent happens, a message is printed to the console.
Copyright © 1997 MageLang Institute. All Rights Reserved.

[ This page was updated: 10-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.