Here you can find the source of setESCCloseable(final JFrame aFrame)
Parameter | Description |
---|---|
aFrame | which needs to be closeable with the ESC key |
@SuppressWarnings("serial") public static void setESCCloseable(final JFrame aFrame)
//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); } }