HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

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).

Figure 1
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.

Author Bio
Jim Mangione, a senior database application developer with an MS in computer science from Drexel University, has nine years of IT experience in the pharmaceutical and chemical industry. He's been involved in both traditional client/server development and distributed application development using Java and application server technology. He can be reached at [email protected]

	

Listing 1: 

public class LoginEvent extends EventObject { 
   private String sUSERNAME; 
   private boolean bVALIDATED; 
  
   LoginEvent(Object source) { 
      super(source); 
   } 
   LoginEvent(Object source, String sUsr, boolean bValidated) { 
         this(source); 
         sUSERNAME = sUsr; 
         bVALIDATED = bValidated; 
   } 
   public String getUsername() { return sUSERNAME; } 
   public boolean getValidated() { return bVALIDATED; } 
} 

Listing 2: 

public addLoginEventListener( LoginEventListener el ) { 
   vListeners.addElement( el ); 
} 
public removeLoginEventListener ( LoginEventListener el ) { 
   vListeners.removeElement( el ); 
} 

Listing 3: 

private void fireValidatedEvent(String sUsr,boolean bValidated) { 
   Vector v; 
   LoginEvent e; 
   v = (Vector) vListeners.clone(); 
   e = new LoginEvent(this, sUsr, bValidated); 
   for (int i=0; i<v.size(); i++) { 
   LoginEventListener el = (LoginEventListener) 
      v.elementAt(i); 
      el.actionValidated(e); } 
} 

Listing 4: 

public class appNetApps extends Applet implements 
LoginEventListener { 
*** 
*** 
bnLoginBean bnLogin = new bnLoginBean(); 
bnLogin.addLoginEventListener( this ); 
// adds this object to the vListeners Vector in the bean 
*** 
*** 
public void actionValidated(LoginEvent e) { 
// code here for handling event when user clicks <OK> on 
// login bean 
} 
public void actionCanceled(LoginEvent e) { 
// code here for handling event when user clicks <Cancel> on 
// login bean 
} 


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.