Most read:
Popular archives:
JavaWorld's new look is here!
We've upgraded the site with a fresh look-and-feel, improved topical navigation, better search, new features, and expanded
community platform. Learn more about the changes to JavaWorld.
| Oracle Compatibility Developer's Guide |
| The Explosion in DBMS Choice |
In Java, it's difficult to intercept key events in the dialog box because the key event goes directly to the component possessing
the focus. To work properly, you have to add the Dialog object as a KeyListener to all its components and containers (and the children and grandchildren of those containers). If you have a lot of complex
Dialogs with deep component hierarchies, this becomes quite a tedious task. Maintenance of the code is also a nightmare, because
when you add a new component, or a container full of components, to an existing Dialog you mustn't forget to add the Dialog as a KeyListener to each new component.
However, there's an easy way to program a response to key events in the Dialog's superclass, so you don't have to remember the Escape key handling when you develop your product further. All you have to
do is create class EscapeDialog that extends the Dialog and automatically adds itself as a KeyListener to all its descendant components. Thus, EscapeDialog will receive notification of all key events that happen in any of its components. So notified, EscapeDialog closes itself when the Escape key is pressed. If you derive all your Dialogs from the EscapeDialog class, hitting the Escape key will close them for you.
The hard part in all this is to add EscapeDialog as a KeyListener to each of its components, because it doesn't know which components belong to its subclasses. Besides, components can be
added and removed on the fly after the Dialog creation. The trick is to make EscapeDialog a ContainerListener so that it's notified when a new component is added to it or to any of its descendant containers. Whenever this happens,
we'll add the EscapeDialog object as a KeyListener to the newly added component and all its descendants.
As a ContainerListener, EscapeDialog must define functions componentAdded and componentRemoved, which will be called whenever a component is added to or removed from a container belonging to the EscapeDialog. However, these functions cannot merely add or remove the EscapeDialog as a listener to the newly added component -- the component could actually be a container full of other components. The EscapeDialog needs to listen to them all. That's why we need to call a recursive function that adds EscapeDialog to the newly added component and all its descendants, if any. Here's the definition of the functions:
// This function is called whenever a Component or a Container is added to
// another Container belonging to this EscapeDialog
public void componentAdded(ContainerEvent e)
{
addKeyAndContainerListenerRecursively(e.getChild());
}
// This function is called whenever a Component or a Container is removed
// from another Container belonging to this EscapeDialog
public void componentRemoved(ContainerEvent e)
{
removeKeyAndContainerListenerRecursively(e.getChild());
}
The two recursive functions called from componentAdded and componentRemoved receive the newly added or removed component as an argument. They do all the work of adding or removing EscapeDialog as a listener to the component and all its descendants. The two functions look exactly the same with the exception that they
handle the opposite actions. If you understand one, you understand both.
Listener object on buttons added dynamically to a container, http://igwe4.vub.ac.be/javacursus/Java095.htm