Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can, or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing heavily in Java's future as a platform for platforms

Also see:

Discuss: Tim Bray on 'What Sun Should Do'

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Turn Java classes into JavaBeans

Reuse your existing Java classes as JavaBeans components

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
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