Java Swing KeyStroke registerAsAction(final KeyStroke keyStroke, final String actionKey, final Runnable action, final JComponent component)

Here you can find the source of registerAsAction(final KeyStroke keyStroke, final String actionKey, final Runnable action, final JComponent component)

Description

register As Action

License

Apache License

Declaration

public static void registerAsAction(final KeyStroke keyStroke,
            final String actionKey, final Runnable action,
            final JComponent component) 

Method Source Code

//package com.java2s;
/*/*from  w ww  .  j  a va2  s. co m*/
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.*;
import java.awt.event.ActionEvent;

public class Main {
    public static void registerAsAction(final KeyStroke keyStroke,
            final String actionKey, final Runnable action,
            final JComponent component) {
        final InputMap inputMap = component
                .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        inputMap.put(keyStroke, actionKey);
        component.getActionMap().put(inputMap.get(keyStroke),
                new AbstractAction() {
                    public void actionPerformed(final ActionEvent e) {
                        action.run();
                    }
                });
    }
}

Related

  1. mapKeyStrokeToButton(final JComponent comp, final String ks, final String name, final AbstractButton button)
  2. maybeInstall(InputMap map, String action, KeyStroke stroke)
  3. refleshAction(JComponent com, KeyStroke keyStroke)
  4. registerAction(JComponent comp, Action action, String key)
  5. registerAction(JComponent component, int condition, KeyStroke key, Action action, String command)
  6. registerKeyBinding(final JComponent aComponent, final String aKeyStroke, final Action aAction)
  7. registerKeyBoardAction(JComponent comp, Action action, KeyStroke stroke)
  8. registerKeyHandler(JComponent component, KeyStroke stroke, final ActionListener listener, final String actionCommand)
  9. registerKeystroke(final JComponent aComponent, final Action aAction, final String aCommandName)