Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Beginner
Nov 10, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Use Escape Key to Close Your AWT Dialogs

It has become customary for Microsoft Windows users to close an active window by pressing the escape key. It is not that easy to intercept key events in Java. This is because key events go directly to the component having the focus. You can always add your Dialog instance as a KeyListener to all its components and containers and the children of containers, and the children of children of containers and ... If you work with multiple Dialogs and deep component hierarhies, the above technique can become a code nightmare.

But, as almost always with Java, there is a better way to do this. You can create a class KeyAwareDialog that extends Dialog and adds itself as a KeyListener to all its components. Then an instance of KeyAwareDialog is notified whenever one of its components receive a key event. Then KeyAwareDialog can close itself if the key event happens to be a escape key event. Now, if you write all your dialogs extending KeyAwareDialog, hitting the escape key will close them.

But, we still haven't solved the problem of adding components to containers in KeyAwareDialog. Also, we need a generic way to deal with components being added and removed to KeyAwareDialog, or its containers at run-time. We can make KeyAwareDialog a ContainerListener. Then, whenever a new component is added to KeyAwareDialog, or any of its descendant containers, it gets notified, and then we can add/remove KeyAwareDialog as a KeyListener to/from the component being added/removed and all its descendants.

So, we let KeyAwareDialog implement the function of java.awt.event.ContainerListener as:

public void componentAdded(ContainerEvent e) 
public void componentRemoved(ContainerEvent e) 
private void addKeyAndContainerListenerRecursively(Component c) 
if(c instanceof Container) 
Container cont = (Container)c; 
Component[] children = cont.getComponents(); 
for(int i = 0; i < children.length; i++) 
Follow the same logic to write removeKeyAndContainerListenerRecursively(...) Now, we implement the java.awt.event.KeyListener for KeyAwareDialog:
public void keyPressed(KeyEvent e) 
int code = e.getKeyCode(); 
if(code == KeyEvent.VK_ESCAPE) 
//Key pressed is the Escape key. Hide this Dialog. 
//If you wan the dialog tpo process other keys, 
//write code following the same logic 
public void keyReleased(KeyEvent e) 
//write code if to handle key release 
public void keyTyped(KeyEvent e) 
//write code if to handle key type 
We're almost there, but no quite. We need to add KeyAwareDialog to itself as a KeyListener. We'll do that in the constructor:
public KeyAwareDialog(Frame frame, String title, boolean modal) 
super(frame, title, modal); 
You can apply this technique to AWT frames too.
Behrouz Fallahi
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date