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:

"Keep listening for upcoming events"

How to wire JavaBeans together using "event listeners"

In Douglas Adams's The Hitchhiker's Guide to the Galaxy, Arthur Dent has a problem:

"You know," said Arthur, "it's at times like this, when I'm trapped in a Vogon airlock with a man from Betelgeuse, and about to die of asphyxiation in deep space, that I really wish I'd listened to what my mother told me when I was young."

"Why, what did she tell you?"

"I don't know, I didn't listen."



JavaBeans can be configured to "listen" to other software objects. And as you'll see, a Java 1.1 class (including any JavaBean) can listen not only to its parent, but also to any class that produces events by becoming an event listener. The event listener idea is central to how Java classes (and other JavaBeans) handle events.

Last month we discussed a specific type of event listener: the PropertyChangeListener interface that takes action when other Beans' properties change. This month we'll take a closer look at the whole concept of an "event listener." You'll see how event listeners are used in the new AWT. (When I say "new," I mean since JDK 1.1 was released.) We'll talk about how to define your own event types, and then make those new event types visible to other classes. Then, as an example, we'll extend the BarChartBean, creating a new event type and then using it to wire the BarChartBean to another Bean. We'll go over some details about how to write event listener interfaces, using the AWT as an example, and conclude with a discussion of inner classes, a new Java 1.1 language feature.

I'd also like to introduce two new icons that will help identify key points:

The JavaBean icon is a key concept for JavaBeans.


And the cuppajoe icon indicates new or key ideas specific to the Java language.


What's an event?

A software event is a piece of data that indicates that something has occurred. Maybe a user moved the mouse. Maybe a datagram just arrived from the network. Maybe a sensor has detected that someone's just come in the back door. All of these occurrences can be modeled as events, and information about what happened can be included in the event. Often it's convenient to write software systems in terms of event processing: Programming then becomes a process of specifying "when this happens, do that." If the mouse moved, move the cursor with it; if a datagram arrived, read it; if there's an intruder, play the recording of Roseanne releasing the rabid Rottweiler.

Usually, an event contains information about the event source (the object that created or first received the event), when the event occurred, and some subclass-specific information that the event receiver can use to figure out what happened and what to do. In a windowing system, for example, there might be a subtype of event for mouse clicks. The mouse click event would include the time the click occurred, and also might include such information as where on the screen the mouse was clicked, the state of the SHIFT and ALT buttons, and an indication of which mouse button was clicked. The code that handles the event is called, strangely enough, the event handler.

1 | 2 | 3 |  Next >
Resources