Java Swing KeyStroke mapKeyStrokeToButton(final JComponent comp, final String ks, final String name, final AbstractButton button)

Here you can find the source of mapKeyStrokeToButton(final JComponent comp, final String ks, final String name, final AbstractButton button)

Description

Adds to JComponent button and keystroke for it.

License

Creative Commons License

Parameter

Parameter Description
comp JComponent
ks KeyStroke
name name
button action

Declaration

public static void mapKeyStrokeToButton(final JComponent comp, final String ks, final String name,
        final AbstractButton button) 

Method Source Code


//package com.java2s;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

public class Main {
    /**//from   ww w .  j  a va 2  s. com
     * Adds to JComponent button and keystroke for it.
     * 
     * @param comp JComponent
     * @param ks KeyStroke
     * @param name name
     * @param button action
     */
    public static void mapKeyStrokeToButton(final JComponent comp, final String ks, final String name,
            final AbstractButton button) {
        comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(ks), name);
        comp.getActionMap().put(name, new AbstractAction() {
            private static final long serialVersionUID = -1;

            public void actionPerformed(ActionEvent evt) {
                button.doClick();
            }
        });
    }
}

Related

  1. installKeystrokes(JComponent component, Action... actions)
  2. isKeyStroke(String propClass)
  3. mapInput(JComponent component, int scope, final int keycode, final int modifiers, final AbstractAction action)
  4. mapKeyStrokeAction(JComponent component, String actionMapKey, Action action, KeyStroke keyStroke)
  5. mapKeyStrokeToAction(final JComponent comp, final String ks, final String name, final AbstractAction action)
  6. maybeInstall(InputMap map, String action, KeyStroke stroke)
  7. refleshAction(JComponent com, KeyStroke keyStroke)
  8. registerAction(JComponent comp, Action action, String key)
  9. registerAction(JComponent component, int condition, KeyStroke key, Action action, String command)