Developer

Develop component-based applications with JavaBeans

Component-based development with Java is fulfilled using the JavaBean technology. This tutorial will help you get a jump start with JavaBeans so you can utilize them in your next application.


Software component technology takes the development process a step further by providing software functionality as components that can be assembled into an application. True components are language-independent and can be manipulated by application builder tools.

You can easily build components in Java using the JavaBean technology, which adds all the capabilities of the Java language, such as ease of use and platform independence, to the equation. Let's take a brief overview of the technology, and then step through the process of creating a simple JavaBean.

How JavaBeans work
The JavaBean technology allows you to develop platform-independent software components using the Java language. A bean (the JavaBean terminology for a component) is a regular Java class and, as such, can be used programmatically as any other class.

However, to be considered a bean, a class must follow some conventions and guidelines so that it can be manipulated by visual application builder tools. Usually beans are visual components derived from the java.awt.Component class or one of its subclasses, but this is not a requirement.

Writing a bean is fairly easy; there is no need to subclass a specific class or implement a specific interface. Beans are defined by a set of features that are exposed by specific conventions. These features fall into three categories:
  • ·        Properties
  • ·        Events
  • ·        Methods

Properties
Properties are named attributes that can be read and/or written. Properties are accessible using accessory methods that allow you to get and set their values. The method to set the value of a property must have the following signature, where <type> is the property’s type and <property> is the property’s name:
 
public void set<property>(<type> newValue)

Likewise, the method to get the value of a property must be:
 
public <type> get<property>()

If the property’s type is Boolean, the signature must be:
 
public Boolean is<property>()

Supposing a bean provides a property named count of the type int, the methods to set and get its value would be:
 
public void setCount( int newValue )
public int getCount()

In addition to properties, events may be used with JavaBeans.

Events
Just like AWT and Swing components, a bean must fire an event to notify interested parties that something has happened. Implementing events in beans requires more effort than properties.

First, you need to define a class derived from the java.util.EventObject class to encapsulate information about the event. By convention, the class name should be <event>Event, where <event> is the event name.

Second, an interface is defined; it is derived from the java.util.EventListener class to be implemented by parties that want to be notified. By convention, the interface name should be <event>Listener. The interface must define one method that receives a single argument of type <event>Event for each notification type.

Last, the bean must provide two methods, one of which allows an object to register itself with the bean to receive notifications, like so:
 
public synchronized void add<event>Listener(<event>Listener l )

The other method is to unregister itself:
 
public synchronized void remove<event>Listener(<event>Listener l )

It is the bean’s responsibility to maintain a list of interested objects (listeners). According to the JavaBean specification, all code that accesses the listener’s list should be synchronized to avoid race conditions caused by multithread applications, hence the synchronized attribute in the method definitions.

Methods
Like any regular Java class, a bean may also define methods. Unlike properties and events, there are no restrictions regarding method signatures, so any names and arguments can be used. Make the methods public, and they will be considered part of the bean’s interface.

A simple example
I developed a simple bean to demonstrate the concepts presented above. The bean is very simple; it counts down the seconds and fires an ActionEvent at zero. The count is displayed using the standard notation (h:mm:ss). It provides two properties:
  • ·        count (int)—The number of seconds to count; as time passes, the count is decremented toward zero
  • ·        running (boolean)—Indicates whether the countdown is running or stopped; setting this property to true starts counting; setting to false stops the countdown

According to the JavaBean specification, the bean provides the methods addActionListener and removeActionListener so other objects can be notified when the countdown is over. Since I used an ActionEvent, I could take advantage of the java.awt.AWTEventMulticaster to manage the listeners.

Also, the bean provides start() and stop() methods to start and stop the countdown, which is the same as setting the running property. Listing A presents the complete code for the sample bean. It presents a simple bean that shows the minimum code needed to create a bean. It can be used as a starting point to create more sophisticated beans.

Sun Microsystems provides a reference implementation of an application builder (called BeanBox) to test beans. It allows you to visually build forms containing beans, set their properties, and connect them through events.

To take advantage of BeanBox, the bean must be packaged in a jar file so it can be loaded into the application builder. Also, the bean must be packaged with a manifest file containing one entry for each bean in the file. Each entry must contain two tags:
  • ·        Name—The path to the bean class file inside the jar file
  • ·        Java-Bean—A Boolean value that must be true

This sidebar shows the manifest file for the CountDown bean.

After packaging the bean, load the jar file into BeanBox and drop two CountDown beans into the form. Notice that CountDown doesn't have an icon because I did not provide it. Set the count property of both to a value other then zero to see how the visual representation of the bean is updated to reflect the new value. After that, connect the two CountDown beans so that the ActionEvent of one calls the start() method of the other. Set the running property of the first bean to true and watch how it starts counting down. When the count is over, an ActionEvent is fired, activating the start() method of the other, which starts counting too. Figure A shows a glimpse of the BeanBox interface.

Figure A
This figure shows two CountDown beans connected through an ActionEvent. When the first count down is over, the second one starts.


Count your beans
JavaBeans merge the power and platform independence of the Java language with software component technology. Once you've learned the JavaBean basic concepts, you can get started with your own beans.

 

Editor's Picks