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
 

Welcome to the Java Q&A Column. Hopefully, this column will evolve into an excellent source of tips and techniques that you'll use over and over again. In each issue, I'll answer questions from readers like you, and offer insights into new and cool things that I've come across in my Java travels.

Since this is a new column, and I don't have any questions yet, I'm going to talk about a common confusion point when it comes to event handling in Java. I hope you enjoy the discussion.

Java Events: True or False?
There seems to be a lot of confusion over the return value in event handlers. Let's take a quick look and set the record straight.

When an event occurs in a Java component the handleEvent() method is called. If you have overridden this method, you've most likely added support for one or more events. When you process these events you're supposed to return a value of true. But what if you don't handle the event? Do you return false?

Well, maybe. It all depends on how you want the event to propagate. Most event models propagate events up the GUI hierarchy. That is to say, if you've got a window that contains a component, the parent window will receive any events that are unhandled by the child component.

In Java, however, you have the option of passing the event up to your parent container. By default, and when you return false from an event handler, the event is passed up, or propagated to, the window hierarchy. Instead, you can propagate the event up the class inheritance hierarchy. This dual event flow is confusing, and when you start mixing the two, watch out!

Let's look at some example code. We've got this program that does something really cool. We track the number of times the user moves the mouse over a special component:

public boolean
mouseEnter( Event event, int x, int y )
{
if ( event.target == mySpecialThing )
{
myHitCounter++;
return( true );
}

return( false );
}

So this counter is working great. Your users love the new counter, everything is wonderful. But then you enhance your program and need to add special event handling with handleEvent():

public boolean
handleEvent( Event event, Object arg )
{
if ( event.target == myOtherThing )
{
// Do some work...
return( true );
}

return( false );
}

All of a sudden, your super-cool mouseEnter handler is never being called. Can you see why? It's because you are returning false from the handleEvent() method. When you return false from any event handler method, the event is passed on to the next level in the window hierarchy. That means that the handleEvent() method was never called in the super class of your component. And that means that the mouseEnter() method was never called either.

So what do you do now? Well, you need to change your handleEvent() to properly call its super class's handleEvent() method. Instead of the line:

return( false );

Use the line:

return( super.handleEvent( event, arg ) );

This will rectify your problem, and future enhancements will work just as well.

In the future, try to think of your event handling as extending the event handling of your base class. When you want to "extend" the handling, always call the super class's event handler on the return. If you want to truly "replace" the event handling, simply return false.

About the Author
Jerry Ablan ([email protected]) is the manager of Internet/intranet Software Development at the Chicago Board Options Exchange. Jerry and his brother Dan ([email protected]), operate NetGeeks (http://www.netgeeks.com), an Internet consulting firm in Chicago, Illinois. Jerry is the author of Developing Intranet Applications with Java from Sams.net; the co-author of the Web Site Administrator's Survival Guide from Sams.net. He also was a contributing author to: Special Edition: Using Java (Que); Platinum Edition Using CGI, HTML, and Java (Que); and Intranets Unleashed (Sams.net).

 

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.