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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as