Java Swing KeyStroke installKey(final JComponent comp, final String keyString, final int key, final Action action)

Here you can find the source of installKey(final JComponent comp, final String keyString, final int key, final Action action)

Description

install Key

License

Open Source License

Declaration

public static void installKey(final JComponent comp, final String keyString, final int key,
            final Action action) 

Method Source Code

//package com.java2s;
/*/*from w w w .  ja  v a2s. c  om*/
 * Copyright (C) 2005 G?rard Milmeister
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

import javax.swing.*;

public class Main {
    public static void installKey(final JComponent comp, final String keyString, final int key,
            final Action action) {
        KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(key, 0, false);
        comp.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(enterKeyStroke, keyString);
        comp.getRootPane().getActionMap().put(keyString, action);
    }
}

Related

  1. getActionForKeystroke(JComponent component, int inputMapId, KeyStroke keyStroke)
  2. getKeyStroke(String s)
  3. getKeyStrokeWithoutCtrlModifier(KeyStroke stroke)
  4. getMnemonic(final String key)
  5. installAction(JComponent comp, Action action, KeyStroke keyStroke, String actionKey)
  6. installKeyBinding(JComponent c, int condition, String actionName, Action action, KeyStroke... keyStrokes)
  7. installKeyBindings()
  8. installKeystrokes(JComponent component, Action... actions)
  9. isKeyStroke(String propClass)