Java tutorial
//package com.java2s; /* * Carrot2 project. * * Copyright (C) 2002-2006, Dawid Weiss, Stanisaw Osiski. * Portions (C) Contributors listed in "carrot2.CONTRIBUTORS" file. * All rights reserved. * * Refer to the full license file "carrot2.LICENSE" * in the root folder of the repository checkout or at: * http://www.carrot2.org/carrot2.LICENSE */ import java.awt.event.*; import javax.swing.*; public class Main { /** * Adds to the dialog a key listener that makes the dialog invisible. * * @param dialog */ public static void addEscapeKeyCloseAction(final JDialog dialog) { dialog.getRootPane().registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent e) { dialog.setVisible(false); } }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); } }