Here you can find the source of closeByESC(final Window window, JPanel panel)
public static void closeByESC(final Window window, JPanel panel)
//package com.java2s; //License from project: Apache License import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import javax.swing.AbstractAction; import javax.swing.JComponent; import javax.swing.JPanel; import javax.swing.KeyStroke; public class Main { public static void closeByESC(final Window window, JPanel panel) { panel.getActionMap().put("close", new AbstractAction() { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent e) { window.dispose();/*from w w w. j a va 2s . co m*/ } }); panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close"); } }