Java JDialog Escape Key addDisposeActionWithEscapeKey(final JDialog dialog)

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

Description

Adds the dispose action with escape key.

License

Open Source License

Parameter

Parameter Description
dialog the dialog

Declaration

public static void addDisposeActionWithEscapeKey(final JDialog dialog) 

Method Source Code

//package com.java2s;
/*/*from   w ww  .ja v a 2s  . com*/
 * aTunes 1.12.0
 * Copyright (C) 2006-2009 Alex Aranda, Sylvain Gaudard, Thomas Beckers and contributors
 *
 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
 *
 * http://www.atunes.org
 * http://sourceforge.net/projects/atunes
 *
 * 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.
 *
 * This program 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 General Public License for more details.
 */

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;

import javax.swing.KeyStroke;

public class Main {
    /**
     * Adds the dispose action with escape key.
     * 
     * @param dialog
     *            the dialog
     */
    public static void addDisposeActionWithEscapeKey(final JDialog dialog) {
        //  Handle escape key to close the dialog

        KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action disposeAction = new AbstractAction() {
            private static final long serialVersionUID = 0L;

            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        };
        dialog.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
        dialog.getRootPane().getActionMap().put("ESCAPE", disposeAction);
    }

    /**
     * Adds the dispose action with escape key.
     * 
     * @param frame
     *            the frame
     */
    public static void addDisposeActionWithEscapeKey(final JFrame frame) {
        //  Handle escape key to close the dialog

        KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action disposeAction = new AbstractAction() {
            private static final long serialVersionUID = 0L;

            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        };
        frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
        frame.getRootPane().getActionMap().put("ESCAPE", disposeAction);
    }
}

Related

  1. actionOnEsc(final JDialog dialog, final Action action)
  2. addCancelByEscapeKey(JDialog fDialog, AbstractAction cancelAction)
  3. addCancelEscape(JDialog f, AbstractAction cancelAction)
  4. addDisposeOnAction(AbstractButton which, final JDialog dia)
  5. addDisposeOnEscape(final JDialog dia)
  6. addEscapeExitListeners(final JDialog window)
  7. addEscapeKeyCloseAction(final JDialog dialog)