Java Swing Key Action registerWindowCloseKeys(JRootPane root, Action closeAction)

Here you can find the source of registerWindowCloseKeys(JRootPane root, Action closeAction)

Description

Registers key events for a Ctrl-W and ESC with an ActionListener that will take care of disposing the window.

License

Open Source License

Parameter

Parameter Description
root The window
closeAction The action to be called

Declaration

public static void registerWindowCloseKeys(JRootPane root,
        Action closeAction) 

Method Source Code

//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);
    }
}

Related

  1. parseKeyStroke(String keyStroke)
  2. pressKey(Component component, int keyCode, int modifier)
  3. registerKey(JComponent component, final int keyEvent, int modifiers, Action action)
  4. registerKeyAction(JComponent component, int keyCode, Action action)
  5. registerTabKey(Container container)
  6. removeTabbedPaneFocusTraversalKeyBindings( JComponent c)
  7. replaceAction(InputMap map, char c)
  8. sendKeys(JComponent component, int modifiers, int key)
  9. setActionID(final Action a, final String id)