Responding to Keystrokes : Various Event Listener « Swing JFC « Java






Responding to Keystrokes

Responding to Keystrokes
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class KeyTester {
  static class MyActionListener extends AbstractAction {
    MyActionListener(String s) {
      super(s);
    }

    public void actionPerformed(ActionEvent e) {
      System.out.println(getValue(Action.NAME));
    }
  }

  public static void main(String args[]) {
    String actionKey = "theAction";
    JFrame f = new JFrame("Key Tester");
    JButton jb1 = new JButton("<html><center>B<br>Focused/Typed");
    JButton jb2 = new JButton("<html><center>Ctrl-C<br>Window/Pressed");
    JButton jb3 = new JButton("<html><center>Shift-D<br>Ancestor/Released");
    Container pane = f.getContentPane();
    pane.add(jb1, BorderLayout.NORTH);
    pane.add(jb2, BorderLayout.CENTER);
    pane.add(jb3, BorderLayout.SOUTH);

    KeyStroke stroke = KeyStroke.getKeyStroke("typed B");
    Action action = new MyActionListener("Action Happened");
    // Defaults to JComponent.WHEN_FOCUSED map
    InputMap inputMap = jb1.getInputMap();
    inputMap.put(stroke, actionKey);
    ActionMap actionMap = jb1.getActionMap();
    actionMap.put(actionKey, action);

    stroke = KeyStroke.getKeyStroke("ctrl C");
    action = new MyActionListener("Action Didn't Happen");
    inputMap = jb2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, actionKey);
    actionMap = jb2.getActionMap();
    actionMap.put(actionKey, action);

    stroke = KeyStroke.getKeyStroke("shift released D");
    action = new MyActionListener("What Happened?");
    inputMap = jb3
        .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(stroke, actionKey);
    actionMap = jb3.getActionMap();
    actionMap.put(actionKey, action);

    f.setSize(200, 200);
    f.show();
  }
}


           
       








Related examples in the same category

1.Demonstrating the WindowListener with a WindowAdapterDemonstrating the WindowListener with a WindowAdapter
2.Demonstrating the ActionListenerDemonstrating the ActionListener
3.Demonstrating the AdjustmentListenerDemonstrating the AdjustmentListener
4.Demonstrating the AncestorListener
5.Demonstrating the ComponentListenerDemonstrating the ComponentListener
6.Demonstrating the ContainerListenerDemonstrating the ContainerListener
7.Demonstrating the FocusListenerDemonstrating the FocusListener
8.Demonstrating the HyperlinkListenerDemonstrating the HyperlinkListener
9.Demonstrating the InternalFrameListenerDemonstrating the InternalFrameListener
10.Demonstrating the ItemListenerDemonstrating the ItemListener
11.Demonstrating the KeyListenerDemonstrating the KeyListener
12.Demonstrating the MenuListenerDemonstrating the MenuListener
13.Demonstrating the MouseListener and MouseMotionListenerDemonstrating the MouseListener and MouseMotionListener
14.Demonstrating the MouseWheelListenerDemonstrating the MouseWheelListener
15.Demonstrating the PopupMenuListenerDemonstrating the PopupMenuListener
16.Demonstrating the WindowListener