Here you can find the source of closeFrameWhenEscapePressed(final JRootPane panel, final ActionListener actListener)
static void closeFrameWhenEscapePressed(final JRootPane panel, final ActionListener actListener)
//package com.java2s; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.JRootPane; import javax.swing.KeyStroke; public class Main { public static final String ESCAPE_ACTION_NAME = "ESCAPE"; static void closeFrameWhenEscapePressed(final JRootPane panel, final ActionListener actListener) { @SuppressWarnings("serial") Action escAction = new AbstractAction(ESCAPE_ACTION_NAME) { public void actionPerformed(ActionEvent evt) { actListener.actionPerformed(evt); }/* w ww. ja v a 2 s . c o m*/ }; escAction.putValue(Action.ACTION_COMMAND_KEY, ESCAPE_ACTION_NAME); KeyStroke escKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escKeyStroke, ESCAPE_ACTION_NAME); panel.getActionMap().put(ESCAPE_ACTION_NAME, escAction); } }