Prerequisites
As a prerequisite, I strongly suggest you review the following JavaWorld articles:
The beans
Although we'll cover seven beans over the course of this series, a few are simply the building blocks of other, more complex
beans. One important feature of beans is that they may themselves be constructed from other beans. What this means is that
we can leverage existing code and conveniently modularize more significant component development efforts, resulting in a more
useful, structured development approach.
As you will see, even the building-block beans are useful for other purposes, which demonstrates another benefit of the JavaBeans approach: As you develop with JavaBeans you build up a library of components that can be reused in future development efforts, saving the time it would have taken to extract or rewrite them.
Let's take a quick look at the beans we'll be implementing in this series.
| AlarmBean | A non-graphical bean that fires off an event after a specified delay. | |
| ArrowBean | A graphical left-arrow/right-arrow bean. | |
| NumberFieldBean | A graphical numeric TextField bean with roll buttons. | |
| ProgressBean | A graphical progress-display bean. | |
| FontChooserBean | A graphical font-chooser bean. | |
| FontSelectorBean |
|
A graphical font-chooser bean that displays a sample of the chosen font and provides OK/Cancel buttons. |
| FontDialogBean |
|
A graphical font-chooser bean that pops up the font selector in a separate dialog. |
We'll only tackle the first two this month. If you want a head start, check out the complete source code for these two beans.
Our scope
For the sake of brevity, we will only be discussing those parts of the classes that are relevant to JavaBeans; internal details
related to threading and the AWT are omitted in all cases. You are invited to read the included source code and the aformentioned
articles if you desire that information.
This month we'll be covering the general principles of JavaBeans programming, including properties and custom events, bean information classes, bean customizers, and the BeanBox. Next month we'll detail the creation of beans from other beans, property listeners, and the programmatic use of JavaBeans in an example application.
For convenience, I've highlighted all the code fragments specific to beans (what I call "beanisms") in red to help direct your focus as you peruse this article.

The AlarmBean bean
This bean introduces two important JavaBeans concepts: properties and events. Properties are the mechanism by which a bean may be customized for a particular application. In this case, the AlarmBean has a "timeout" property that corresponds to a delay: after the alarm is started, it will sleep for this delay before firing its event. Events are the mechanism by which beans are hooked together. An event represents a significant occurrence from a bean (in this case, the alarm going off). A JavaBeans application is created by hooking events up to actions. For example, when the alarm goes off an event is fired, which can start an action like an animation sequence.
The following classes comprise this bean:
AlarmBean -- The main bean class
AlarmEvent -- The bean's event class
AlarmListener -- The listener class for delivering alarm events
AlarmBeanBeanInfo -- The bean information class
AlarmBeanCustomizer -- A graphical customization class
Class AlarmBean
The AlarmBean class is the basic bean class. We'll start off with a quick overview of how the class is constructed, then we'll look at
the beanisms:
public class AlarmBean implements Runnable, Serializable ...
We inherit from Object and implement Runnable to make use of a timer thread.
protected int timeout = 1000; public void setTimeout (int t) ... public int getTimeout () ...
An internal variable timeout holds the alarm timeout value, which is accessible through the setTimeout() and getTimeout() methods.
transient protected Thread alarm; public synchronized void start () ... public synchronized void stop () ... public void run () ...
The start() method creates a new Thread alarm which enters the run() method, sleeps for the specified period, and then fires an alarm event. We can call the stop() method to abort the alarm prematurely.