Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
We're going to learn to "beanify" an applet by example. The applet we're going to beanify, ColorFadeBar, produces a title bar (perhaps for a Web page), with a color gradient, like this:
![]()
Figure 1: ColorFadeBar applet in action
You can see the full source code for this applet here. You may want to open another browser window (or print the applet) so you can follow along with this discussion.
The first task in beanifying an existing class (whether that class is an applet or not) is to identify what attributes of
the class can be considered properties. (See my previous article on customization in the Resources below.) Properties allow the user of a class to customize how the class appears or behaves. For example, Figure 2 shows some
attributes of the ColorFadeBar applet that are programmable (via the <PARAM> tags within an <APPLET> tag in HTML).

Figure 2: Programmable attributes of a ColorFadeBar
The font, font size, color, and alignment of the text message are all parameterized, as are the beginning and ending colors
for the cool background fade effect, and the width and height of the applet. There are also two parameters (X0 and DY, not shown here; see Table 1 below) that allow you to nudge the text vertically or horizontally for various purposes.
These attributes of the applet aren't properties yet. A JavaBeans property consists of methods that allow the retrieval and/or modification of some internal state of a class. The applet currently doesn't have methods that provide that functionality.
Although applet parameters are often directly translatable to properties (since their purpose usually is to affect the appearance
or behavior of an object), they are fundamentally different mechanisms. An applet accesses a parameter by calling java.applet.Applet.getParameter(), and then converting whatever string is returned to the appropriate type. A property, on the other hand, consists of a method
or methods that the class exposes (that is, makes publicly available) in order to allow external agents to access encapsulated information.
If you used parameters while writing your applet, you were probably already thinking in terms of properties. In fact, the
parameters to your applet are your first candidates for the properties of your new bean. Our ColorFadeBar applet has the following parameters:
| Parameter | Meaning |
|---|
| WIDTH | The width of the applet |
| HEIGHT | The height of the applet |
| STARTCOLOR | The beginning color for the background fade |
| ENDCOLOR | The ending color for the background fade |
| TEXTCOLOR | The color in which to render the message |
| TEXT | The message text |
| FADEDIR | Background fade direction (left, right, up, down) |
| TEXTDIR | Text alignment (left, center, right) |
| FONT | The name of the font for TEXT |
| FONTSIZE | The size of the font for TEXT |
| X0 | Text left or right margin offset from applet edge (depending on TEXTDIR; default 5) |
| DY | Text baseline offset (default 0) |
These parameters can be used to customize how the applet works. We first need to identify what the properties of the new bean will be, and then add accessor functions for them. Table 2 shows the properties I've chosen for the new bean, a description of each property, and the parameter or parameters to which the property corresponds.
| Property name | Type | Description | Parameter(s) |
|---|---|---|---|
| Height | int | Height of the applet | HEIGHT |
| Width | int | Width of the applet | WIDTH |
| Message | String | The message to display | TEXT |
| MessageFont | Font | The font of the message | FONT, FONTSIZE |
| TextDirection | int | The text alignment | TEXTDIR |
| MessageColor | Color | The color of the text | TEXTCOLOR |
| ColorFrom | Color | The color at which fade begins | STARTCOLOR |
| ColorTo | Color | The color at which fade ends | ENDCOLOR |
| FadeDirection | int | The direction in which color fades | FADEDIR |
Notice that some of the property names differ from their corresponding parameter names. There is no programmatic relationship
between parameters and properties. In practice, it's probably best to give properties the same name as their corresponding
parameters, but here I've changed them to demonstrate that they're entirely independent from parameters. Also, notice that
one new property we're going to add, MessageFont, corresponds to more than one applet parameter. Another difference between parameters and properties is that applet parameters
are all strings. It's frequently necessary to convert many of these properties from their string representations to some other
type -- for example (in the ColorFadeBar source code):
protected int ixGetParameter(String sName, int iDefault)
{
int i;
try {
// Conversion from string to int occurs here
i = Integer.parseInt(getParameter(sName), 16);
}
catch (NumberFormatException e) { i = iDefault; }
return i;
}
...
// Conversion from int to Color occurs here
if ((i = ixGetParameter("TEXTCOLOR", 0xffffff)) >= 0)
_colorText = new Color(i);
In the JavaBean we're about to write, the MessageColor property will be accessible as type Color: No translation is necessary!
If the class you're turning into a bean isn't an applet (and therefore doesn't have parameters), don't worry. You can still examine the class and imagine what attributes you'd like to be able to customize if the class were a bean. For example, a class that accesses a database and returns a list of tables in the database is much more reusable if the name and location of the database can be configured by calling property accessors.
Once you've identified what properties your bean should have, the next step is to actually create the properties.
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq