inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "OK"); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel"); targetComponent.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap); targetComponent.getActionMap().put("OK", actionOK); targetComponent.getActionMap().put("Cancel", actionCancel);
im.put(up, focusPrevKey); im.setParent(c.getInputMap(JComponent.WHEN_FOCUSED)); c.setInputMap(JComponent.WHEN_FOCUSED, im);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "OK"); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel"); targetComponent.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap); targetComponent.getActionMap().put("OK", actionOK); targetComponent.getActionMap().put("Cancel", actionCancel);
/** * Associated the specified acion with key presses in the specified * compoent. * * @param component * @param theAction */ public static void setKeyCatcher(final JComponent component, final AbstractAction theAction) { InputMap oldInputMap = component .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); InputMap newInputMap = new InputMap(); newInputMap.setParent(oldInputMap); newInputMap.put(KeyStroke.getKeyStroke("F1"), "doSomething"); component.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, newInputMap); ActionMap oldActionMap = component.getActionMap(); ActionMap newActionMap = new ActionMap(); newActionMap.setParent(oldActionMap); newActionMap.put("doSomething", theAction); component.setActionMap(newActionMap); }
/** * Associated the specified acion with key presses in the specified * compoent. * * @param component * @param theAction */ public static void setKeyCatcher(final JComponent component, final AbstractAction theAction) { InputMap oldInputMap = component .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); InputMap newInputMap = new InputMap(); newInputMap.setParent(oldInputMap); newInputMap.put(KeyStroke.getKeyStroke("F1"), "doSomething"); component.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, newInputMap); ActionMap oldActionMap = component.getActionMap(); ActionMap newActionMap = new ActionMap(); newActionMap.setParent(oldActionMap); newActionMap.put("doSomething", theAction); component.setActionMap(newActionMap); }
comp.setInputMap(JComponent.WHEN_FOCUSED, newInputMap); term.setKeyStrokeSet((HashSet) passKeystrokes);
/** * Adds the specified input map to the specified component. * * @param c * Component. * @param map * Input map to add. */ void addUIInputMap(JComponent c, InputMap map) { InputMap lastNonUI = null; InputMap parent = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); while (parent != null && !(parent instanceof UIResource)) { lastNonUI = parent; parent = parent.getParent(); } if (lastNonUI == null) { c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map); } else { lastNonUI.setParent(map); } map.setParent(parent); }
component.setInputMap(condition, im);
static void addKeyboardSupport( final ScreenDisplayPresenter presenter){ final JComponent panel = presenter.getView(); panel.setInputMap( JComponent.WHEN_FOCUSED, BUTTON.getInputMap()); panel.addFocusListener( new FocusAdapter() {
frame.getRootPane().setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap); frame.setVisible(true);
panel.setInputMap( JComponent.WHEN_FOCUSED, map);
/** * Removes the specified input map from the specified component. * * @param c * Component. * @param map * Input map to remove. */ void removeUIInputMap(JComponent c, InputMap map) { InputMap im = null; InputMap parent = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); while (parent != null) { if (parent == map) { if (im == null) { c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map .getParent()); } else { im.setParent(map.getParent()); } break; } im = parent; parent = parent.getParent(); } }
protected void addListeners(JComponent comp) { if (comp == null) { return; } if (this instanceof MouseListener) { comp.addMouseListener((MouseListener) this); } if (this instanceof MouseMotionListener) { comp.addMouseMotionListener((MouseMotionListener) this); } if (this instanceof MouseWheelListener) { comp.addMouseWheelListener((MouseWheelListener) this); } // Keystrokes comp.addKeyListener(this); comp.setActionMap(createActionMap(keyActionMap)); comp.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, createInputMap(keyActionMap)); }
InputMap new_im = new InputMap(); new_im.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, InputEvent.CTRL_DOWN_MASK), "toggle between views"); cpane.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, new_im);