Use Escape Key to Close Your AWT Dialogs

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) { addKeyAndContainerListenerRecursively(e.getChild()); } and public void componentRemoved(ContainerEvent e) { removeKeyAndContainerListenerRecursively(e.getChild()); } private void addKeyAndContainerListenerRecursively(Component c) { c.addKeyListener(this); if(c instanceof Container) { Container cont = (Container)c; cont.addContainerListener(this); Component[] children = cont.getComponents(); for(int i = 0; i < children.length; i++) { addKeyAndContainerListenerRecursively(children[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. setVisible(false); } //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); addKeyAndContainerListenerRecursively(this); } 

You can apply this technique to AWT frames too.

See also  5 Benefits of Using Time Tracking Software

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist