Digital Edition

SYS-CON.TV
Adding a Custom Event to a JavaBean
Adding a Custom Event to a JavaBean

One of the first truly reusable components I wrote in Java was a login bean that validated a username/password against our company's network. It was lightweight (using AWT classes), and worked with both applets and applications.

This proved to me how simple writing and deploying a JavaBean was. It also introduced me quickly to the concept of custom event handling. You see, the bean's container class (be it an applet, frame, etc.) can easily talk to the bean and invoke public methods such as bnLogin.setDefaultUserName(). But after the bean validates a user, it has to notify its container whether the user is valid or invalid through the use of a custom event.

In JDK 1.1, if you use only standard GUI events such as mouseClicked() or keyPressed(), they're inherited from the java.awt.AWTEvent, which superseded the java.awt.Event class used in JDK 1.0. If a TextField component wants to inform its container class that a user just typed a character into it, there's a standard event for that, keyPressed(), which most of today's IDEs will set up for you. However, if a custom component such as our login bean wants to inform its container class that a user has just been validated - actionValidated(), for instance - a new event must be written.

Along with the JDK 1.1 java.awt.AWTEvent class came the introduction of the java.util.EventListener and java.util.EventObject classes. These exist to provide custom event handling. To use them is a four-step process.

Step 1
In Listing 1 a new event is created by extending the EventObject class, which will hold the username and validated status when the bean fires a login event.

Step 2
A new listener is created as an interface that extends the EventListener class, which contains the methods that pass the LoginEvent object to all registered listeners, as shown below:

public interface LoginEventListener extends EventListener {
public void actionValidated(LoginEvent e);
public void actionCanceled(LoginEvent e); }

The container class (such as an applet) will implement this listener and the two defined methods. The login bean will store a reference to the container class (stored in a vector) and, using that reference, call the appropriate method whenever an event is fired (see Figure 1).

Step 3
Next, our login bean will need to be modified to create LoginEvents and notify the container classes of an event via the methods defined in the LoginEventListener. It must also provide public methods to add and remove the container classes. Don't worry. I can assure you it's really not as bad as it sounds!

We'll start with how to register the container classes that will listen to events fired by the bean. As shown in Listing 2, an addLoginEventListener() and removeLoginEventListener() are added as public methods to the bean. These maintain the vListeners Vector, which holds LoginEventListener objects. There will be one element in this Vector for each listener.

The two events required by our login bean to be fired are when a user selects the <ok> button, actionValidated(), and if the user cancels the login, actionCanceled(). The easiest way to do this is to have two private methods called fireValidatedEvent() and fireCanceledEvent(). After the user has been validated or denied access, I call the callfireValidateEvent() method or the fireCanceledEvent() method inside the mouse-click event of the cancel button. Listing 3 shows an example of the fireValidatedEvent. This loops through each element in the vListeners Vector, casting the element to the LoginEventListener class and calling the actionValidated() method of that object.

The same code is used for the fireCanceledEvent(), only el.actionValidated() is replaced with el.actionCanceled().

Step 4
The login bean is now firing off events for any objects that implement the LoginEventListener class and register with the bean. So how do we listen for these events? The container class must implement the LoginEventListener class, register itself with the bean and define the methods from the listener. This is detailed in Listing 4.

By following these four steps, you can have bidirectional communication between your custom bean and the container classes that hold them.

About Jim Mangione
Jim Mangione is Director of Software Engineering for the Report & Query
division of CareScience, Inc. located in Philadelphia, PA. He has over
12 years experience in client/server and web-based application
development and holds a Masters Degree in Computer Science from Drexel
University. He can be reached at jmangione@carescience.com.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1



ADS BY GOOGLE
Subscribe to the World's Most Powerful Newsletters

ADS BY GOOGLE

Modern software design has fundamentally changed how we manage applications, causing many to turn to...
In this presentation, you will learn first hand what works and what doesn't while architecting and d...
In an era of historic innovation fueled by unprecedented access to data and technology, the low cost...
In his session at 20th Cloud Expo, Mike Johnston, an infrastructure engineer at Supergiant.io, discu...
Everyone wants the rainbow - reduced IT costs, scalability, continuity, flexibility, manageability, ...
DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing w...
Founded in 2000, Chetu Inc. is a global provider of customized software development solutions and IT...
DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novemb...
Most DevOps journeys involve several phases of maturity. Research shows that the inflection point wh...
SYS-CON Events announced today that DatacenterDynamics has been named “Media Sponsor” of SYS-CON's 1...
CloudEXPO New York 2018, colocated with DXWorldEXPO New York 2018 will be held November 11-13, 2018,...
Dynatrace is an application performance management software company with products for the informatio...
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, ...
@DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22...
Today, we have more data to manage than ever. We also have better algorithms that help us access our...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: D...
A valuable conference experience generates new contacts, sales leads, potential strategic partners a...
DXWorldEXPO LLC announced today that ICOHOLDER named "Media Sponsor" of Miami Blockchain Event by Fi...
SYS-CON Events announced today that IoT Global Network has been named “Media Sponsor” of SYS-CON's @...
The best way to leverage your Cloud Expo presence as a sponsor and exhibitor is to plan your news an...