JButton: getActionMap() : JButton « javax.swing « Java by API






JButton: getActionMap()

  
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

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 MainClass {

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

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

           
         
    
  








Related examples in the same category

1.Button.background
2.Button.font
3.Button.foreground
4.new JButton(Action act)
5.new JButton(Icon ic)
6.new JButton(String text)
7.new JButton(String str) (HTML String)
8.new JButton(String text, Icon icon)
9.JButton: addActionListener(ActionListener act)
10.JButton: addAncestorListener(AncestorListener listener)
11.JButton: addChangeListener(ChangeListener l)
12.JButton: addItemListener(ItemListener l)
13.JButton: addPropertyChangeListener(PropertyChangeListener lis)
14.JButton: contains(int x, int y)
15.JButton: createToolTip()
16.JButton: getAccessibleContext()
17.JButton: getBackground()
18.JButton: getInputMap()
19.JButton: removeActionListener(ActionListener l)
20.JButton: setActionCommand(String text)
21.JButton: setAlignmentX(float alignmentX)
22.JButton: setAlignmentY(float alignmentY)
23.JButton: setBackground(Color c)
24.JButton: setBorder(Border border)
25.JButton: setBorderPainted(boolean b)
26.JButton: setDefaultCapable(boolean defaultCapable)
27.JButton: setDisabledIcon(Icon disabledIcon)
28.JButton: setDisabledSelectedIcon(Icon disabledSelectedIcon)
29.JButton: setHorizontalAlignment(int alignment)
30.JButton: setIconTextGap(int iconTextGap)
31.JButton: setMnemonic(KeyEvent.VK_A)
32.JButton: setPressedIcon(Icon pressedIcon)
33.JButton: setRolloverIcon(Icon rolloverIcon)
34.JButton: setRolloverSelectedIcon(Icon rolloverSelectedIcon)
35.JButton: setSelectedIcon(Icon selectedIcon)
36.JButton: setToolTipText(String str)
37.JButton: setVerticalAlignment(int alignment)
38.extends JButton