Example usage for javax.swing JButton getActionMap

List of usage examples for javax.swing JButton getActionMap

Introduction

In this page you can find the example usage for javax.swing JButton getActionMap.

Prototype

public final ActionMap getActionMap() 

Source Link

Document

Returns the ActionMap used to determine what Action to fire for particular KeyStroke binding.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component = new JButton("button");
    ActionMap map = component.getActionMap();
    list(map, map.keys());//from  w w  w.j av a  2 s  . c o  m
    list(map, map.allKeys());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component = new JButton();
    MyAction action = new MyAction();
    component.getActionMap().put(action.getValue(Action.NAME), action);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component = new JButton();
    MyAction action = new MyAction();

    component.getActionMap().put(action.getValue(Action.NAME), action);
}

From source file:Main.java

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);

}

From source file:Main.java

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);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component = new JButton("Button");
    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");

    component.getActionMap().put("actionName", new AbstractAction("actionName") {
        public void actionPerformed(ActionEvent evt) {
            System.out.println(evt);
        }/*from   w  ww  .  jav  a 2  s  .c o m*/
    });

}

From source file:KeyTester.java

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);/*from ww  w  .  ja va  2s .c om*/
    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();
}

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }//  www  .j  a  v  a2  s . c o m
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();
    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }//from  ww w  .  j  ava  2  s  . c  o  m
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();
    Object[] keys = actionMap.allKeys();
    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }//from ww  w  .j a  v  a  2s.  c o m
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();
    actionMap.clear();
    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

    frame.setSize(400, 200);
    frame.setVisible(true);
}