import java.awt.event.*;import javax.swing.*;public class EscapeDialog extends JDialog{ public static void main(String[]args) { new EscapeDialog(); } private JTextField input = null; public EscapeDialog() { setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); createCloseAction(); initTextField(); pack(); setLocationRelativeTo(null); setVisible(true); } private void initTextField() { input = new JTextField(10); getContentPane().add(input); } private void createCloseAction() { Action closeAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { //do what ever you want dispose(); } }; KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "closeDialog"); getRootPane().getActionMap().put("closeDialog", closeAction); }}