Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
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
This article throws light on the problems inherent in traditional event-handling implementations using AWT/JFC and demonstrates six ways to get around them.
Note: This article assumes that the reader has a thorough understanding of the AWT event model. Experience with the Reflection API is necessary to modify or extend the five utility classes provided in this article.
Before getting into the guts of the system, let's take a quick peek at the event model mechanism in AWT and JFC.
The event source (typically a UI component) emits events through a predefined listener interface. Any object interested in
those events will register an implementation of the listener with the event source through an addXXXListener() method. Except for DocumentEvent, the rest of the events derive from the java.util.EventObject class. This is the essence of the event model; anything else is implementation detail. To get some background on the AWT/JFC
event model, see the Resources section at the end of the article.
There are some outstanding issues with current event-handling implementations:
EventHandler connects the event source and target object at the object levelEventListener connects the event source and target objects at the method level using a predefined listener classGenericListener connects the event source and target objects at the method level by dynamically creating a custom listener implementation
at runtime
To improve readability of the article, I have chosen to present a very simple UI implementation. Let's walk through it using
a simple program that displays two toggling buttons in a Frame. The program also handles the system-close event to dispose of the Frame. The same example is presented for each of the utility classes listed above. The following three example programs use standard
JDK/AWT classes.
bottomButtonAction method and topButtonAction method, respectively. Similarly, the window closing event is handled by calling the System.exit() method.package test.event;
import java.awt.*; import java.awt.event.*;
/** Uses Anonymous Classes to handle events. */ public class ButtonTest1 extends Frame { Button top = new Button("Toggle bottom button"); Button bottom = new Button("Toggle top button");
public ButtonTest1() { super("ButtonTest1 uses Anonymous Classes"); setLayout(new BorderLayout()); add("North", top); add("Center", bottom);
bottom.setEnabled(true); top.setEnabled(false);
bottom.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { bottomButtonAction(e); } });
top.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { topButtonAction(e); } });
this.addWindowListener(new WindowAdapter() { public void windowClosing(ActionEvent e) { System.exit(0); } });
pack(); show(); }
public void topButtonAction(ActionEvent event) { Button b = (Button)event.getSource(); bottom.setEnabled(true); top.setEnabled(false); } public void bottomButtonAction(ActionEvent event) { Button b = (Button)event.getSource(); bottom.setEnabled(false); top.setEnabled(true); } public static void main(String[] args) { new ButtonTest1(); } }
This approach also generates anonymous inner classes (the total compiled class size is high). In general, the logic behind the event handling should be extremely simple and small so as to avoid maintenance and readability issues.
Proxy"Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq