Java JDialog Escape Key closeOnEscape(final JDialog dialog)

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

Description

Make the dialog close when the used presses escape.

License

Open Source License

Parameter

Parameter Description
dialog dialog to make obey the escape key

Declaration

public static void closeOnEscape(final JDialog dialog) 

Method Source Code

//package com.java2s;
/***************************************************************************
 *                   (C) Copyright 2003-2012 - Stendhal                    *
 ***************************************************************************
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

import java.awt.Window;
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.InputMap;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

public class Main {
    /** A key that should not get mixed with anything else. */
    private static final String WINDOW_CLOSE = "org.stendhalgame:window_closing";

    /**//from   w  w  w . j  av  a  2s.c o m
     * Make the dialog close when the used presses escape. The event will be
     * the same as when the user closes the window using the window manager.
     * 
     * @param dialog dialog to make obey the escape key
     */
    public static void closeOnEscape(final JDialog dialog) {
        closeOnEscape(dialog, dialog.getRootPane());
    }

    /**
     * Make the window close when the used presses escape. The event will be
     * the same as when the user closes the window using the window manager.
     * 
     * @param frame window to make obey the escape key
     */
    static void closeOnEscape(final JFrame frame) {
        closeOnEscape(frame, frame.getRootPane());
    }

    /**
     * Make the window close when the used presses escape. The event will be
     * the same as when the user closes the window using the window manager.
     * 
     * @param window window to make obey the escape key
     * @param root the root container of the window
     */
    private static void closeOnEscape(final Window window, final JRootPane root) {
        InputMap map = root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WINDOW_CLOSE);
        Action dispatchClosing = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent event) {
                window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
            }
        };
        root.getActionMap().put(WINDOW_CLOSE, dispatchClosing);
    }
}

Related

  1. addEscapeToCloseSupport(final JDialog dialog)
  2. addEscapeToCloseSupport(final JDialog dialog, final boolean fadeOnClose)
  3. addEscKeyAction(javax.swing.JDialog dialog, javax.swing.Action action)
  4. closeOnEsc(final JDialog dlg)
  5. closeOnEsc(JDialog dialog)
  6. closeOnEscapePressed(final JDialog dialog)
  7. decorate(final JDialog d, boolean closeOnEscape)
  8. enableCloseByEscape(final JDialog dialog)
  9. installEscapeCloseOperation(final JDialog dialog)