Java Swing Key Action installCloseKey(final RootPaneContainer c)

Here you can find the source of installCloseKey(final RootPaneContainer c)

Description

Install a key listener on a window to close it.

License

Open Source License

Parameter

Parameter Description
c a parameter

Declaration

public static void installCloseKey(final RootPaneContainer c) 

Method Source Code


//package com.java2s;
/*/*  w w  w. j a  v a  2  s  .co  m*/
 * Copyright 2011 StackFrame, LLC
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * as published by the Free Software Foundation.
 *
 * You should have received a copy of the GNU General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import javax.swing.RootPaneContainer;

public class Main {
    /**
     * Install a key listener on a window to close it. (e.g., Control-W on Windows, Command-W on Mac).
     *
     * @param c
     */
    public static void installCloseKey(final RootPaneContainer c) {
        ActionListener closer = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                ((Component) c).setVisible(false);
            }
        };
        JRootPane rootPane = c.getRootPane();
        Toolkit toolkit = ((Component) c).getToolkit();
        rootPane.registerKeyboardAction(closer,
                KeyStroke.getKeyStroke(KeyEvent.VK_W, toolkit.getMenuShortcutKeyMask()),
                JComponent.WHEN_IN_FOCUSED_WINDOW);
    }
}

Related

  1. getPrettyStringFor(KeyStroke keyStroke)
  2. getSystemHelpKey()
  3. getTypePanel(final ActionListener typeListener)
  4. handleSliderAdjustmentViaKey(KeyEvent e)
  5. initButton(String text, String actionKey, int shortcutKey, int modifiers, JComponent component, AbstractAction action)
  6. invoke(Action action, Object source)
  7. isActionSelected(Action action)
  8. isKeyStrokeEvent(@Nullable final KeyStroke keyStroke, final int keyEventType, @Nullable final KeyEvent event)
  9. isValidKey(int keyCode)