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

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Turn Java classes into JavaBeans

Reuse your existing Java classes as JavaBeans components

Like many Java programmers, you've invested a lot of time and effort into your existing Java classes. You feel rightly proud of what you've accomplished. True, some of your classes may have been written for JDK 1.0.2, or even earlier, but they still run just fine, thank you very much. Wouldn't it be great if you could dust off those old applets and classes and repackage them as shiny new JavaBeans? Well, the good news is, you can. While some of the JavaBeans Specification depends on JDK 1.1 functionality, class files written to JDK 1.0.2 can still be modified to run as beans.

Beanify your applet

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.

Task 1: Identify your properties

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)
Table 1. Parameters to existing applet


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
Table 2. Properties of the new JavaBean


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.

1 | 2 | 3 |  Next >
Resources
  • The JavaBeans Advisor has a short article on turning applets into beans http://java.sun.com/beans/advisor/Advisor_2.html
  • Sun also provides instructions for developing JavaBeans under JDK 1.0.2. This information is useful in converting old Java classes to new beans, if the classes will still have to run in the old JDK 1.0.2 JVM. This information appears at http://java.sun.com/beans/docs/initial.html
  • You can download source files for this article in Unix TAR format /javaworld/jw-06-1998/beans/jw-06-beans.tar
  • Or download source files in ZIP format /javaworld/jw-06-1998/beans/jw-06-beans.zip
  • You can also download the JAR file for this article at /javaworld/jw-06-1998/beans/jw-06-beans.jar
  • "JavaBeans book review"
    We picked three winners -- now let's see how they stack up http://www.javaworld.com/javaworld/jw-05-1998/jw-05-beans.html
  • "Serialization grab bag"
    Answers to reader questions about serialization http://www.javaworld.com/javaworld/jw-04-1998/jw-04-beans.html
  • "It's in the contract! Object versions for JavaBeans"
    Use object versioning to maintain serialization compatibility with your JavaBeans http://www.javaworld.com/javaworld/jw-03-1998/jw-03-beans.html
  • "Serialization and the JavaBeans Specification"
    The trick to controlling and -- when necessary -- preventing serialization http://www.javaworld.com/javaworld/jw-02-1998/jw-02-beans.html
  • "Do it the "NescafÈ" way -- with freeze-dried JavaBeans"
    How to use object serialization for bean persistence http://www.javaworld.com/javaworld/jw-01-1998/jw-01-beans.html
  • "Soup up your Java classes with customization"
    Wow your beans users with cool customization features http://www.javaworld.com/javaworld/jw-12-1997/jw-12-beans.html
  • "The trick to controlling bean customization"
    Make your beans easy to customize using the BeanInfo interface and the Introspector class http://www.javaworld.com/javaworld/jw-11-1997/jw-11-beans.html
  • "Keep listening for upcoming events"
    How to wire together JavaBeans using "event listeners" http://www.javaworld.com/javaworld/jw-10-1997/jw-10-beans.html
  • "The BeanBoxSun's JavaBeans test container"
    Learn how to use this valuable testing tool http://www.javaworld.com/javaworld/jw-09-1997/jw-09-beanbox.html
  • "Double Shot, Half Decaf, Skinny Latte" -- Customize your Java
    How to tailor JavaBeans to fit your application http://www.javaworld.com/javaworld/jw-09-1997/jw-09-beans.html
  • "A walking tour of JavaBeans"
    What JavaBeans is, how it works, and why you want to use it http://www.javaworld.com/javaworld/jw-08-1997/jw-08-beans.html