Here you can find the source of registerWindowCloseKeys(JRootPane root, Action closeAction)
Parameter | Description |
---|---|
root | The window |
closeAction | The action to be called |
public static void registerWindowCloseKeys(JRootPane root, Action closeAction)
//package com.java2s; /*/*w w w. j a va 2 s . c o m*/ * This file is part of the Alchemy project - http://al.chemy.org * * Copyright (c) 2007-2010 Karl D.D. Willis * * Alchemy 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. * * Alchemy 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 Alchemy. If not, see <http://www.gnu.org/licenses/>. * */ import javax.swing.*; public class Main { /** * Registers key events for a Ctrl-W and ESC with an ActionListener * that will take care of disposing the window. * @param root The window * @param closeAction The action to be called */ public static void registerWindowCloseKeys(JRootPane root, Action closeAction) { // Shortcut to close with escape // root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Close Window"); // Shortcut to close with a modifier - w // root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_W, KEY_MODIFIER), "Close Window"); // Assign the action for the two keys // root.getActionMap().put("Close Window", closeAction); } }