List of usage examples for javax.swing JComponent WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
To view the source code for javax.swing JComponent WHEN_ANCESTOR_OF_FOCUSED_COMPONENT.
Click Source Link
registerKeyboardAction
that means that the command should be invoked when the receiving component is an ancestor of the focused component or is itself the focused component. 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 a2s . c o 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); }
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 . java 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.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()); }/*from w w w. j a v a 2s . 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(); 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()); }/*from w ww . jav a2s. 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); System.out.println(newMap.getParent()); 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: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 . j a v a 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
/** * Binds an action to the dialog so when the user presses the ESCAPE key, the dialog is hidden. * /*from w w w . j a va 2 s.co m*/ * @param dialog * the dialog to bind the action to. */ public static void bindEscapeAction(final JDialog dialog) { InputMap iMap = dialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape"); ActionMap aMap = dialog.getRootPane().getActionMap(); aMap.put("escape", new AbstractAction() { public void actionPerformed(ActionEvent e) { dialog.setVisible(false); } }); }
From source file:Main.java
public static void refleshAction(JComponent com, KeyStroke keyStroke) { InputMap im = com.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); Object o = im.get(keyStroke); if (o == null) { im = com.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); o = im.get(keyStroke);/* w ww . j a v a2 s .c om*/ } if (o != null) { Action a = com.getActionMap().get(o); a.setEnabled(a.isEnabled()); } }
From source file:Main.java
public Main() { JComboBox cpmbo = new JComboBox(); cpmbo.addItem("One"); cpmbo.addItem("Two"); cpmbo.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put( KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, KeyEvent.ALT_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK), "spacePopup"); this.add(cpmbo); }
From source file:Main.java
/** * Obtine actiunea inregistrata pentru apasarea de tasta * @return actiune inregistrata pentru tasta sau null *///from w ww .j av a 2 s .c o m public static Action getActionForKeystroke(JComponent component, KeyStroke keyStroke) { Action whenFocused = getActionForKeystroke(component, JComponent.WHEN_FOCUSED, keyStroke); Action whenAncestorOfFocused = getActionForKeystroke(component, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, keyStroke); Action whenInWindow = getActionForKeystroke(component, JComponent.WHEN_IN_FOCUSED_WINDOW, keyStroke); //Ordinea preferata pentru a le returna if (whenFocused != null) return whenFocused; if (whenAncestorOfFocused != null) return whenAncestorOfFocused; if (whenInWindow != null) return whenInWindow; //Valoare default return null; }
From source file:MainClass.java
MainClass(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jp = new JPanel(); Vector v = new Vector(); v.add("A");/* www . j a va 2s.com*/ v.add("B"); v.add("C"); jcb = new JComboBox(v); jcb.setEditable(true); KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, Event.CTRL_MASK); jcb.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(ks, "clearEditor"); jcb.getActionMap().put("clearEditor", new ClearEditorAction()); jp.setPreferredSize(new Dimension(200, 35)); jp.add(jcb); getContentPane().add(jp); pack(); setVisible(true); }