Here you can find the source of mapKeyStrokeToButton(final JComponent comp, final String ks, final String name, final AbstractButton button)
Parameter | Description |
---|---|
comp | JComponent |
ks | KeyStroke |
name | name |
button | action |
public static void mapKeyStrokeToButton(final JComponent comp, final String ks, final String name, final AbstractButton button)
//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(); } }); } }