Java JFrame addDisposeActionWithEscapeKey(final JFrame frame)

Here you can find the source of addDisposeActionWithEscapeKey(final JFrame frame)

Description

Adds the dispose action with escape key.

License

Open Source License

Parameter

Parameter Description
frame the frame

Declaration

public static void addDisposeActionWithEscapeKey(final JFrame frame) 

Method Source Code

//package com.java2s;
/*/*w ww  .j  a 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. abrirJFrameCentralizado(JFrame dialog)
  2. activateWindowClosingButton(JFrame frame)
  3. addEscapeExitListeners(final JFrame window)
  4. addEscapeListener(final JFrame frame)
  5. addJFrameReporter(JFrame to)
  6. addPanelToFrame(final JFrame frame, final JPanel panel, final String title)