Here you can find the source of addEscapeListener(final JDialog dialog)
public static void addEscapeListener(final JDialog dialog)
//package com.java2s; //License from project: Open Source License import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; public class Main { public static void addEscapeListener(final JDialog dialog) { ActionListener escListener = new ActionListener() { @Override//from w w w . jav a2s . com public void actionPerformed(ActionEvent e) { dialog.setVisible(false); } }; dialog.getRootPane().registerKeyboardAction(escListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); } }