Java JDialog Escape Key installEscapeCloseOperation(final JDialog dialog)

Here you can find the source of installEscapeCloseOperation(final JDialog dialog)

Description

Add a keyboard shortcut to allow ESC to close the dialog.

License

Open Source License

Parameter

Parameter Description
dialog The dialog to be updated.

Declaration

public static void installEscapeCloseOperation(final JDialog dialog) 

Method Source Code

//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);
    }
}

Related

  1. closeOnEscapePressed(final JDialog dialog)
  2. decorate(final JDialog d, boolean closeOnEscape)
  3. enableCloseByEscape(final JDialog dialog)
  4. installEscapeCloseOperation(final JDialog dialog)
  5. installEscapeCloseOperation(final JDialog dialog)
  6. installEscapeKey(final JDialog comp)
  7. registerEscapeKey(final JDialog dialog, ActionListener actionListener)
  8. setEscapeAction(JDialog dialog, Action action)
  9. setEscapeClosable(JDialog dialog)