Here you can find the source of installEscapeCloseOperation(final JDialog dialog)
Parameter | Description |
---|---|
dialog | The dialog to be updated. |
public static void installEscapeCloseOperation(final JDialog dialog)
//package com.java2s; /*/*from w w w .j av a2 s . c o m*/ * Copyright 2002-2003 (C) B. K. Oxley (binkley) <binkley@alumni.rice.edu> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.WindowEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JRootPane; import javax.swing.KeyStroke; public class Main { private static final KeyStroke escapeStroke = KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0); /** * An action map key for the user requesting a dialog close via the ESC key. */ private static final String dispatchWindowClosingActionMapKey = "pcgen:WINDOW_CLOSING"; /** * Add a keyboard shortcut to allow ESC to close the dialog. * * @param dialog The dialog to be updated. */ public static void installEscapeCloseOperation(final JDialog dialog) { JRootPane root = dialog.getRootPane(); root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( escapeStroke, dispatchWindowClosingActionMapKey); Action dispatchClosing = new AbstractAction() { @Override public void actionPerformed(ActionEvent event) { dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING)); } }; root.getActionMap().put(dispatchWindowClosingActionMapKey, dispatchClosing); } }