Here you can find the source of addEscapeExitListeners(final JFrame window)
public static void addEscapeExitListeners(final JFrame window)
//package com.java2s; //License from project: Open Source License import java.awt.Component; import java.awt.Container; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JDialog; import javax.swing.JFrame; public class Main { public static void addEscapeExitListeners(final JFrame window) { addKeyAdapterRecursively(window, new KeyAdapter() { @Override//from w ww.j ava2 s . c o m public void keyPressed(final KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { System.exit(0); } } }); } public static void addEscapeExitListeners(final JDialog window) { addKeyAdapterRecursively(window, new KeyAdapter() { @Override public void keyPressed(final KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { System.exit(0); } } }); } public static void addKeyAdapterRecursively(final Container container, final KeyAdapter keyAdapter) { container.addKeyListener(keyAdapter); for (int i = 0; i < container.getComponentCount(); i++) { final Component child = container.getComponent(i); if (child instanceof Container) { addKeyAdapterRecursively((Container) child, keyAdapter); } child.addKeyListener(keyAdapter); } } }