List of usage examples for javax.swing JButton getInputMap
public final InputMap getInputMap()
InputMap
that is used when the component has focus. 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); }/* w w w . j a v a 2s .co 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);//w w w. ja va 2s. c o m 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 w w w .j a v a 2 s . co 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 w ww . j a v a 2s . co 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); }
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()); }//w w w. j a va 2 s .c om }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); Object[] keys = actionMap.keys(); 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()); }/* www. j a va 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(); System.out.println(actionMap.size()); 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 w ww. ja 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(); Object[] keys = actionMap.keys(); for (Object key : keys) { actionMap.get(key); } 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()); }// 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(); Object[] keys = actionMap.keys(); for (Object key : keys) { actionMap.remove(key); } 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()); }/*w w w. ja va 2 s. co m*/ }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); ActionMap newMap = new ActionMap(); newMap.setParent(actionMap); 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); }