Java Tutorial/Swing Event/InputMap

Материал из Java эксперт
Перейти к: навигация, поиск

Adding an InputMap to a Component

import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
public class Main {
  public static void main(String[] argv) throws Exception {
    InputMap inputMap = new InputMap();
    inputMap.put(KeyStroke.getKeyStroke("F2"), "actionName");
    JButton component = new JButton("button");
    
    inputMap.setParent(component.getInputMap(JComponent.WHEN_FOCUSED));
    component.setInputMap(JComponent.WHEN_FOCUSED, inputMap);
  }
}





Disable a character so that no action is invoked

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class Main {
  public static void main(String[] argv) {
    JTextField component = new JTextField(10);
    
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke("typed X"), "none");

    JFrame f = new JFrame();
    f.add(component);
    f.setSize(300, 300);
    f.setVisible(true);
  }
}





InputMap javax.swing.JComponent.getInputMap(int condition)

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) throws Exception {
    JButton component = new JButton();
    MyAction action = new MyAction();
    component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F2"),
        action.getValue(Action.NAME));
  }
}
class MyAction extends AbstractAction {
  public MyAction() {
    super("my action");
  }
  public void actionPerformed(ActionEvent e) {
    System.out.println("hi");
  }
}





Install your own action to text component

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
class InsertAction extends AbstractAction {
  public InsertAction() {
    super("Insert Space");
  }
  public void actionPerformed(ActionEvent evt) {
    JTextComponent c = (JTextComponent) evt.getSource();
    try {
      c.getDocument().insertString(c.getCaretPosition(), " space", null);
    } catch (BadLocationException e) {
    }
  }
}
public class Main {
  public static void main(String[] argv) {
    JTextField component = new JTextField(10);
    InsertAction insertSpaceAction = new InsertAction();
    component.getInputMap(JComponent.WHEN_FOCUSED).put(
        KeyStroke.getKeyStroke(new Character(" "), 0), "none");
    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("pressed SPACE"),
        insertSpaceAction.getValue(Action.NAME));
    component.getActionMap().put(insertSpaceAction.getValue(Action.NAME), insertSpaceAction);
    JFrame f = new JFrame();
    f.add(component);
    f.setSize(300, 300);
    f.setVisible(true);
  }
}





Modify a component: press space bar or F2 to move focus to next focusable component.

import java.awt.ruponent;
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();
  }
}





Overriding Default Typed Key Bindings in a JTextComponent

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class Main {
  public static void main(String[] argv) {
    JTextField component = new JTextField(10);
    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("typed a"),
        "actionName");
    JFrame f = new JFrame();
    f.add(component);
    f.setSize(300, 300);
    f.setVisible(true);
  }
}





Press shift space or shift F2 to move the focus to the previous focusable component.

import java.awt.ruponent;
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();
    PrevFocusAction prevFocusAction = new PrevFocusAction();
    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("shift F2"),
        prevFocusAction.getValue(Action.NAME));
    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("shift SPACE"),
        prevFocusAction.getValue(Action.NAME));
    component.getActionMap().put(prevFocusAction.getValue(Action.NAME), prevFocusAction);
  }
}
class PrevFocusAction extends AbstractAction {
  PrevFocusAction() {
    super("Move Focus Backwards");
  }
  public void actionPerformed(ActionEvent evt) {
    ((Component) evt.getSource()).transferFocusBackward();
  }
}





Prevent the space from being inserted when shift-space is pressed

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class Main {
  public static void main(String[] argv) {
    JTextField component = new JTextField(10);
    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(new Character(" "), 0), "none");
    JFrame f = new JFrame();
    f.add(component);
    f.setSize(300, 300);
    f.setVisible(true);
  }
}





Sharing an InputMap or an ActionMap Between Two Components

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.text.JTextComponent;
public class Main {
  public static void main(String[] argv) {
    InputMap im = new JTextArea().getInputMap(JComponent.WHEN_FOCUSED);
    im.put(KeyStroke.getKeyStroke("F2"), "actionName");
    ActionMap am = new JTextArea().getActionMap();
    am.put("actionName", new AbstractAction("actionName") {
      public void actionPerformed(ActionEvent evt) {
        System.out.println((JTextComponent) evt.getSource());
      }
    });
    im.put(KeyStroke.getKeyStroke("F3"), "actionName2");
    am.put("actionName2", new AbstractAction("actionName2") {
      public void actionPerformed(ActionEvent evt) {
        System.out.println((JTextComponent) evt.getSource());
      }
    });
    JButton component1 = new JButton("button 1");
    JButton component2 = new JButton("button 2");
    component1.setInputMap(JComponent.WHEN_FOCUSED, im);
    component2.setInputMap(JComponent.WHEN_FOCUSED, im);
    component1.setActionMap(am);
    component2.setActionMap(am);

  }
}





void InputMap.put(KeyStroke keyStroke, Object actionMapKey)

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) throws Exception {
    JButton component = new JButton();
    MyAction action = new MyAction();
    component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F2"),
        action.getValue(Action.NAME));
  }
}
class MyAction extends AbstractAction {
  public MyAction() {
    super("my action");
  }
  public void actionPerformed(ActionEvent e) {
    System.out.println("hi");
  }
}