Here you can find the source of addEscapeListener(final JFrame frame)
public synchronized static void addEscapeListener(final JFrame frame)
//package com.java2s; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.KeyStroke; public class Main { public synchronized static void addEscapeListener(final JFrame frame) { final ActionListener escListener = new ActionListener() { @Override/* ww w. jav a 2 s . c o m*/ public void actionPerformed(final ActionEvent e) { frame.dispose(); } }; frame.getRootPane().registerKeyboardAction(escListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); } }