Professional Documents
Culture Documents
Growing Java Beans: A Code-Based Intro To Java's Component Architecture
Growing Java Beans: A Code-Based Intro To Java's Component Architecture
Growing Java Beans: A Code-Based Intro To Java's Component Architecture
by Andrew Downs
Edited by the MacTech Editorial Staff
Introduction
JavaBeans is the Java component architecture. This article discusses and demonstrates how to
write several simple components (hereafter referred to as Beans). The reader should have a
general familiarity with Java. The code in this article was developed using the Apple Mac OS
Runtime for Java (MRJ) version 2.0 and the MRJ SDK 2.0.1 ea2.
For additional background information, several related articles previously published in MacTech
are listed in the reference section at the end of this article. Several books are also listed.
Overview
A Java Bean bears a strong resemblance to a well-written Java application. But since it is a
component, a Bean's scope is usually smaller than that of an entire application, making it easier
to develop.
expose accessor methods (e.g. getValue() and setValue()) to allow retrieval and changing
of attribute values by external sources;
allow for easy mixing and matching via GUI development tools;
generate or respond to appropriate events;
save their state using the Java Serialization mechanism.
In this article, we will explore the code behind two relatively simple Beans. They contain enough
of the above-listed features to make them interesting, while remaining easy to read and
understand. One of the Beans displays a sequence of lights similar to a U.S.-style traffic light.
The other Bean changes the sequence of the light display. We will look at the code for these
classes later.
Figure 1 shows the two Beans running within a Java Frame object. (A Frame is a platform-
specific window.) Figure 2 shows the Beans running inside the BeanBox, a GUI tool from
JavaSoftthat allows you to instantiate, connect, and test Bean behavior. The BeanBox is provided
free of charge from JavaSoft, as part of the Beans Development Kit (BDK). Since it is written in
Java, the BeanBox can be installed on the Macintosh using the MRJ Software Development Kit
tools. It can then be run the same as other Java applications. The URL for obtaining the BeanBox
is provided at the end of this article. (Note: you will need a file-extraction program that can
open .zip files in order to unpack the BeanBox.)
Notice that there is little difference in the visible display of the Beans, whether running
standalone or in the BeanBox. One Bean appears as a Java Choice menu (a Mac OS popup
menu), and the other is a rectangle containing three circles, one of which is filled at any given
time.
Global Values
This class defines some global values that are shared between the Display and ModeSelector
classes. These values are collected in one place to simplify housekeeping and maintenance. We
will refer to them from the other classes as Global.NORMAL, Global.sleep, etc. This is the Java
syntax for referencing static (class) attributes. Note that final means the values cannot be
changed after they are initially assigned, so these are constants.
Listing 1: Global.java
Global.java
// Shared values.
Choice Bean
The ModeSelector Bean is a Choice menu component (a popup menu on the Mac). When the
user changes the currently selected item, the Bean notifies any registered listeners. (We will see
that the DisplayFrame class instantiates a Display object and registers it as a listener on a
ModeSelector object.)
Listing 2: ModeSelector.java
ModeSelector.java
// A Choice menu class for changing the display mode (and resulting light
sequence).
ModeSelector specifies that it implements the ItemListener interface so that it can receive item
changed events (generated when the user makes a selection). The method that must be defined
for the ItemListener interface is itemStateChanged(). In this implementation, ModeSelector
ignores the event object contents, and checks its own state instead.
// Mode value.
private int mode = Global.NORMAL;
public ModeSelector() {
// Call the superclass constructor.
super();
// Starting mode.
this.setMode( Global.NORMAL );
ModeSelector uses its own accessor methods to get and set the mode value. This may seem like
overkill, since a class can access its own variables (even private ones, such as mode) directly.
However, it is arguably a good habit, since variable references from outside the class should go
through accessor methods, and never directly access variable values.
Note that ModeSelector does not define any unique serialization that needs to occur. Instead, it
uses the superclass' implementation.
Center of Attention
This Bean defines the lights that will be drawn as part of the traffic light. It also contains a
Thread which is used to time the repaints.
Note that Display inherits directly from java.awt.Component. This makes it a "lightweight"
component (as well as a Bean), which simply means that it has no native window object
associated with it at runtime, and also none of the overhead associated with such an object.
Listing 3: Display.java
Display.java
// The traffic light class, which sequences and draws the colored lights.
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.util.*;
// Size matters.
private int width = 50, height = 165;
public Display() {
// Call the superclass constructor.
super();
The runner object used in this class, in conjunction with the Runnable interface, allows Display
to take on the behavior of a Thread without actually subclassing directly from Thread. In the
run() method, the runner is put to sleep temporarily. On wakeup, it compares the current time to
the reference time, and if the reference time has been exceeded, this object's state gets updated
(i.e. the light color may change).
while ( true ) {
try {
// Sleep for <interval> milliseconds.
Thread.sleep( this.interval );
}
catch ( InterruptedException ex ) {
System.out.println( "InterruptedException..." );
}
The following method gets called from run() approximately every second. The timing used here
is simple: in normal mode, each light will be "on" for five seconds; in maintenance mode, that
interval is reduced to one second. In terms of color, normal mode sequences the lights (green,
then yellow, then red), while maintenance mode only flashes the yellow light.
if ( this.getMode() == Global.NORMAL ) {
// In normal mode, each light stays on for 5 seconds.
if ( this.count > 4 ) {
this.count = 0;
repaint();
}
}
else if ( mode == Global.MAINT ) {
// In maintenance mode, the light stays on for 1 second.
if ( this.count > 0 ) {
this.count = 0;
repaint();
}
}
}
if ( this.getMode() == Global.NORMAL ) {
// For any light, black signifies "off".
g.setColor( Color.black );
When retrieving the object's state, the runner object must be created from scratch, since the
Thread class is not serializable.
Container App
The DisplayFrame class is provided as a container app for the other Beans we've built. It is a
Frame containing the Display (traffic light) and ModeSelector. Figure 1 shows the Beans
running inside a DisplayFrame object. Since DisplayFrame is an application, it can run
independently of the BeanBox.
Listing 4: DisplayFrame.java
DisplayFrame.java
// A container app for runtime.
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.util.*;
public DisplayFrame() {
// Call the superclass constructor.
super();
Add the Display object as a listener on the ModeSelector, so that it will be notified when the
"mode" value changes.
ms.addPropertyChangeListener(
( PropertyChangeListener )display );
this.add( p );
this.setVisible( true );
}
The archive for this article includes the .java and .class files (in separate directories), and
individual .jar files containing each Bean and Global.class. In addition, the manifest directory
contains manifest files for each of the classes, for use in building JAR files. You can also
combine the manifests and classes into one big JAR file.
To run the program, drag the file DisplayFrame.class onto the JBindery application icon, which
is located in the MRJ SDK JBindery folder. Once JBindery launches, it will display
"DisplayFrame" in the class name field. (This field specifies the name of the class to run at
application startup; that class must contain a main() method.) Click OK to run the program. To
run inside the BeanBox, add the .jar files to the jars directory on your hard drive. Then, launch
the BeanBox application. It should open and read the .jar files, and display the Beans in the
palette on the left side. It will write an error message to the console stating that Global.jar does
not contain any Beans. This is not a problem, since we know that Global is not a Bean, but rather
a supporting class.
Conclusion
Java Beans should be reusable, customizable, and packaged in JAR files. As Bean development
tools become more widespread, developers will find it even easier to create custom apps by
combining Beans in new ways. Bean development allows an incremental, flexible approach
which should make it easy for all developers to participate.
References
Developing Java Beans, Robert Englander, O'Reilly & Associates, Inc., 1997.
Java in a Nutshell, David Flanagan, O'Reilly & Associates, Inc., 1997.
Exploring Java, Patrick Niemeyer and Joshua Peck, O'Reilly & Associates, Inc., 1997.
Java Serialization, Andrew Downs, MacTech Magazine, April 1998.
Building Beans, Will Iverson, MacTech Magazine, June 1997.
URLs
http://developer.apple.com/java/
http://www.lists.apple.com/mrj.html
http://java.sun.com/products/index.html