Here you can find the source of addEscapeListener(final JDialog dialog)
static public void addEscapeListener(final JDialog dialog)
//package com.java2s; // This copy of Ice is licensed to you under the terms described in the import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import javax.swing.JDialog; import javax.swing.JComponent; import javax.swing.KeyStroke; public class Main { static public void addEscapeListener(final JDialog dialog) { dialog.getRootPane().registerKeyboardAction(new ActionListener() { @Override//from w ww . java 2s .c o m public void actionPerformed(ActionEvent e) { dialog.dispose(); } }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); } }