Java JFrame setESCCloseable(final JFrame aFrame)

Here you can find the source of setESCCloseable(final JFrame aFrame)

Description

makes the JFrame closeable with ESC key.

License

Open Source License

Parameter

Parameter Description
aFrame which needs to be closeable with the ESC key

Declaration

@SuppressWarnings("serial")
public static void setESCCloseable(final JFrame aFrame) 

Method Source Code

//package com.java2s;
/*//from ww  w. j  av a 2  s.  c  o  m
 * CustomerSoft
 * Copyright (C) 2012  Michael Kohler <michaelkohler@linux.com>
    
 * 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 3 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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.JFrame;
import javax.swing.KeyStroke;

public class Main {
    /**
     * makes the JFrame closeable with ESC key.
     * 
     * @param aFrame which needs to be closeable with the ESC key
     */
    @SuppressWarnings("serial")
    public static void setESCCloseable(final JFrame aFrame) {
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction() {
            public void actionPerformed(ActionEvent ae) {
                aFrame.dispose();
            }
        };
        aFrame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        aFrame.getRootPane().getActionMap().put("ESCAPE", escapeAction);

    }
}

Related

  1. setCursorFree(JFrame frame)
  2. setDialogLocation(JFrame frame, JFrame parentFrame)
  3. setDirty(JFrame frame, boolean isDirty)
  4. setEscapeAction(JFrame frame, Action action)
  5. setEscapeClosable(JFrame frame)
  6. setFrameBottomRight(final JFrame frame)
  7. setFramePositon(JFrame inTargetFrame)
  8. setFrameTitle(JFrame frame, File file)
  9. setGeneralParameters(final JFrame window)