Here you can find the source of closeOnEsc(final JDialog dlg)
public static void closeOnEsc(final JDialog dlg)
//package com.java2s; /* Copyright (C) 2013 Interactive Brokers LLC. All rights reserved. This code is subject to the terms * and conditions of the IB API Non-Commercial License or the IB API Commercial License, as applicable. */ import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import javax.swing.AbstractAction; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.KeyStroke; public class Main { /** Configure dialog to close when Esc is pressed. */ public static void closeOnEsc(final JDialog dlg) { dlg.getRootPane().getActionMap().put("Cancel", new AbstractAction() { public void actionPerformed(ActionEvent e) { dlg.dispose();/*ww w . j a va2 s . c o m*/ } }); dlg.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel"); } }