Here you can find the source of actionOnEsc(final JDialog dialog, final Action action)
public static void actionOnEsc(final JDialog dialog, final Action action)
//package com.java2s; //License from project: Open Source License import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; public class Main { public static void actionOnEsc(final JDialog dialog, final Action action) { KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); Action escapeAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { action.actionPerformed(null); }/* w w w . j ava2 s . c om*/ }; dialog.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE"); dialog.getRootPane().getActionMap().put("ESCAPE", escapeAction); } }