Adding an InputMap to a Component - Java Swing

Java examples for Swing:Event

Description

Adding an InputMap to a Component

Demo Code

import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class Main {
  public static void main(String[] argv) {
    InputMap inputMap = new InputMap();

    // Add a KeyStroke
    inputMap.put(KeyStroke.getKeyStroke("F2"), "actionName");
    /*from  ww  w .j a  v  a2s  .  com*/
    JTextField component = null;
    inputMap.setParent(component.getInputMap(JComponent.WHEN_FOCUSED));
    component.setInputMap(JComponent.WHEN_FOCUSED, inputMap);
  }
}

Related Tutorials