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:

Java Tip 35: Create new event types in Java

Learn how to make event classes for components in JDK 1.1

While JDK 1.1 certainly has streamlined event handling with the introduction of the delegation event model, it does not make it easy for developers to create their own event types. The basic procedure described here is actually rather straightforward. For the sake of simplicity, I will not discuss concepts of event enabling and event masks. Plus, you should know that events created using this procedure will not be posted to the event queue and will work only with registered listeners.

Currently, the Java core consists of 12 event types defined in java.awt.events:

  • ActionEvent
  • AdjustmentEvent
  • ComponentEvent
  • ContainerEvent
  • FocusEvent
  • InputEvent
  • ItemEvent
  • KeyEvent
  • MouseEvent
  • PaintEvent
  • TextEvent
  • WindowEvent


Because creating new event types is a non-trivial task, you should examine events that are part of core Java. If possible, try to use those types rather than create new ones.

There will be times, however, when a new event type will need to be developed for a new component. For purposes of this discussion, I will use the example of a simple component, a wizard panel, as a means to demonstrate how to create a new event type.

A wizard panel implements a simple wizard interface. The component consists of a card panel that can be advanced using the NEXT button. The BACK button allows you to flip to the previous panel. FINISH and CANCEL buttons are also provided.

In order to make the component flexible, I wanted to provide full control over the actions taken by all the buttons to the developer who uses it. For example, when the NEXT button is pressed, it should be possible for the developer to first check if the required data was entered on the component currently visible before advancing to the next component.

There are five main tasks in creating your own event type:

  • Create an event listener

  • Create a listener adapter

  • Create an event class

  • Modify the component

  • Managing multiple listeners
We'll examine each of these tasks in turn and then put them all together.

Create an event listener

One way (and there are many) of informing objects that a certain action has occurred is to create a new event type that could be delivered to registered listeners. In the case of the wizard panel, a listener should support four different event cases, one for each button.

I start by creating a listener interface. For each button, I define a listener method in the following fashion:

import java.util.EventListener;
public interface WizardListener extends EventListener {
    public abstract void nextSelected(WizardEvent e);
    public abstract void backSelected(WizardEvent e);
    public abstract void cancelSelected(WizardEvent e);
    public abstract void finishSelected(WizardEvent e);
}


Each method takes one argument: WizardEvent, which is defined next. Note that the interface extends EventListener, used to identify this interface as an AWT listener.

Create a listener adapter

Creating a listener adapter is an optional step. In AWT, a listener adapter is a class that provides a default implementation for all methods of a certain listener type. All adapter classes in the java.awt.event package provide empty methods that do nothing. Here is an adapter class for WizardListener:

1 | 2 | 3 | 4 |  Next >
Resources