JComponent: getActionMap()
import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
public class Main {
public static void main(String[] argv) {
JButton component = new JButton();
NextFocusAction nextFocusAction = new NextFocusAction();
component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("SPACE"),
nextFocusAction.getValue(Action.NAME));
component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("F2"),
nextFocusAction.getValue(Action.NAME));
component.getActionMap().put(nextFocusAction.getValue(Action.NAME), nextFocusAction);
}
}
class NextFocusAction extends AbstractAction {
public NextFocusAction() {
super("Move Focus Forwards");
}
public void actionPerformed(ActionEvent evt) {
((Component) evt.getSource()).transferFocus();
}
}
Related examples in the same category