Creating a KeyStroke and Binding It to an Action - Java Security

Java examples for Security:Key

Description

Creating a KeyStroke and Binding It to an Action

Demo Code

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.KeyStroke;

public class Main {

  public void m() {
    JButton component = null;/*from   w w  w  .j a va 2s . c o  m*/
    // Create some keystrokes and bind them all to the same action
    component.getInputMap().put(KeyStroke.getKeyStroke("F2"), "actionName");
    component.getInputMap().put(KeyStroke.getKeyStroke("control A"),"actionName");
    component.getInputMap().put(KeyStroke.getKeyStroke("shift F2"),"actionName");
    component.getInputMap().put(KeyStroke.getKeyStroke('('), "actionName");
    component.getInputMap().put(KeyStroke.getKeyStroke("button3 F"),"actionName");
    component.getInputMap().put(KeyStroke.getKeyStroke("typed x"), "actionName");
    component.getInputMap().put(KeyStroke.getKeyStroke("released DELETE"),"actionName");
    component.getInputMap().put(KeyStroke.getKeyStroke("shift UP"),"actionName");

    // Add the action to the component
    component.getActionMap().put("actionName",
        new AbstractAction("actionName") {
          public void actionPerformed(ActionEvent evt) {
            System.out.println(evt);
          }
        });
  }
}

Related Tutorials