public static void addKeyBinding(JComponent comp, KeyStroke key, String id, Action action) { comp.getInputMap().put(key, id); comp.getActionMap().put(id, action); }
textArea.setLineWrap( true ); textArea.setWrapStyleWord( true ); KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER"); textArea.getInputMap().put(keyStroke, "none"); JScrollPane scrollPane = new JScrollPane( textArea ); getContentPane().add( scrollPane ); buttons.add( ok ); buttons.add( cancel ); getContentPane().add(buttons, BorderLayout.SOUTH); pack(); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible(true);
public static void bindKeyToAction(int keyCode, int modifiers, Action action, JComponent component, int condition) { KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, modifiers); component.getInputMap(condition).put(keyStroke, action); }
public class Main { public static final void addKeyBinding(JComponent c, String key, final Action action) { c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), key); c.getActionMap().put(key, action); c.setFocusable(true); } public static void main(String[] args) { final JFrame frame = new JFrame("Fullscreen Toggle Test"); Container contentPane = frame.getContentPane(); contentPane.add(new JLabel("Toogle fullscreen using F11"), BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 400); frame.setVisible(true); addKeyBinding(frame.getRootPane(), "F11", new FullscreenToggleAction(frame)); } }
static void wrapUpDownArrowActions(JComponent inplaceEditor, final IncrementPropertyValueSupport incrementSupport) { InputMap im = inplaceEditor.getInputMap( JComponent.WHEN_FOCUSED ); wrapAction( im.get(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)), inplaceEditor.getActionMap(), incrementSupport, true ); wrapAction( "selectPrevious", inplaceEditor.getActionMap(), incrementSupport, true ); //NOI18N wrapAction( "selectPrevious2", inplaceEditor.getActionMap(), incrementSupport, true ); //NOI18N wrapAction( im.get(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0)), inplaceEditor.getActionMap(), incrementSupport, false ); wrapAction( "selectNext", inplaceEditor.getActionMap(), incrementSupport, false ); //NOI18N wrapAction( "selectNext2", inplaceEditor.getActionMap(), incrementSupport, false ); //NOI18N }
private void setExitOnEscWhenEnabled(JComponent mainComponent) { Action escapeAction = new AbstractAction() { private static final long serialVersionUID = -3460391555954575248L; @Override public void actionPerformed(ActionEvent e) { if (luytenPrefs.isExitByEscEnabled()) { quit(); } } }; KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); mainComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(escapeKeyStroke, "ESCAPE"); mainComponent.getActionMap().put("ESCAPE", escapeAction); }
private static void setKeyboardShortcut(JComponent comp, Object actionName, char stroke) { comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(stroke), actionName); }
/** * Uninstalls actions and key bindings from linkPanel. Does nothing if the * linkPanel is null. * * @param panel the component to uninstall * */ protected void uninstallLinkPanelKeyboardActions(JComponent panel) { if (panel == null) return; ActionMap map = panel.getActionMap(); map.remove(JXDatePicker.HOME_COMMIT_KEY); map.remove(JXDatePicker.HOME_NAVIGATE_KEY); InputMap inputMap = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); // PENDING: get from LF inputMap.remove(KeyStroke.getKeyStroke("F5")); inputMap.remove(KeyStroke.getKeyStroke("shift F5")); }
ActionMap actionMap = getActionMap(); int condition = JComponent.WHEN_IN_FOCUSED_WINDOW; InputMap inputMap = getInputMap(condition); inputMap.put(direction.getKeyStroke(), direction.getText()); actionMap.put(direction.getText(), new MyArrowBinding(direction.getText())); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); UP("Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)), DOWN("Down", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)), LEFT("Left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)), RIGHT("Right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));
private static void registerKeyPressAction(final JComponent component, final int keyCode, final Runnable action) { component.getInputMap().put(KeyStroke.getKeyStroke(keyCode, 0, false), SwingAction.of("", e -> action.run())); }
this.add(b); f.getRootPane().setDefaultButton(b); this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), EXIT); this.getActionMap().put(EXIT, exit); this.addMouseMotionListener(new MouseAdapter() { f.setResizable(false); f.setUndecorated(true); f.add(this); f.pack(); dev.setFullScreenWindow(f);
p.add(f); frame.add(p); frame.setVisible(true); textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "Down released"); textField.getActionMap().put("Down released", new AbstractAction() { @Override public void actionPerformed(ActionEvent ae) {//focuses the first label on popwindow suggestionsPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "Down released"); suggestionsPanel.getActionMap().put("Down released", new AbstractAction() { int lastFocusableIndex = 0; autoSuggestionPopUpWindow.setVisible(false); setFocusToTextField(); suggestionsPanel.add(suggestionLabel); getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), "Enter released"); getActionMap().put("Enter released", new AbstractAction() { @Override public void actionPerformed(ActionEvent ae) {
this.add(new MoveButton("\u2190", KeyEvent.VK_LEFT, -DELTA, 0)); this.add(new MoveButton("\u2191", KeyEvent.VK_UP, 0, -DELTA)); this.add(new MoveButton("\u2192", KeyEvent.VK_RIGHT, DELTA, 0)); this.add(new MoveButton("\u2193", KeyEvent.VK_DOWN, 0, DELTA)); final int dx, final int dy) { super(name); this.k = KeyStroke.getKeyStroke(code, 0); this.dx = dx; this.dy = dy; ControlPanel.this.getInputMap(WHEN_IN_FOCUSED_WINDOW) .put(k, k.toString()); ControlPanel.this.getActionMap() .put(k.toString(), new AbstractAction() { f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); contentPane.add( nullTraversalKeys() ); contentPane.add( writeYourOwnAction() ); contentPane.add( useKeyListener() ); contentPane.add( addTraversalKeys() ); JScrollPane scrollPane = new JScrollPane( textArea ); InputMap im = textArea.getInputMap(); KeyStroke tab = KeyStroke.getKeyStroke("TAB"); textArea.getActionMap().put(im.get(tab), new TabAction(true)); KeyStroke shiftTab = KeyStroke.getKeyStroke("shift TAB"); im.put(shiftTab, shiftTab); textArea.getActionMap().put(im.get(shiftTab), new TabAction(false)); set.add( KeyStroke.getKeyStroke( "TAB" ) ); textArea.setFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set ); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
setSize(500, 500); setLayout(new BorderLayout()); setVisible(true); panel = new JPanel(); panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "forward"); panel.getActionMap().put("forward", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { add(panel, BorderLayout.CENTER);
InputMap inputMap = getInputMap(condition); ActionMap actionMap = getActionMap(); for (final Direction dir : Direction.values()) { KeyStroke keyStroke = KeyStroke.getKeyStroke(dir.getKeyCode(), 0, false); inputMap.put(keyStroke, dir.name() + KEY_DOWN); actionMap.put(dir.name() + KEY_DOWN, new AbstractAction() { keyStroke = KeyStroke.getKeyStroke(dir.getKeyCode(), 0, true); inputMap.put(keyStroke, dir.name() + KEY_RELEASE); actionMap.put(dir.name() + KEY_RELEASE, new AbstractAction() { frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true);
InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap actMap = getActionMap(); for (final Direction direction : Direction.values()) { KeyStroke pressed = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, false); KeyStroke released = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, true); inMap.put(pressed, direction.toString() + "pressed"); inMap.put(released, direction.toString() + "released"); actMap.put(direction.toString() + "pressed", new AbstractAction() { actMap.put(direction.toString() + "released", new AbstractAction() { frame.getContentPane().add(new MoveIcon()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
/** * uninstalls and deregisters the click action from the component's * actionMap/inputMap. * */ protected void unregisterExecuteButtonAction() { component.getActionMap().put(EXECUTE_BUTTON_ACTIONCOMMAND, null); KeyStroke space = KeyStroke.getKeyStroke("released SPACE"); component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put( space, null); }
protected void _init(JComponent component, XTable table) { _table = table; _value = null; // Pressing 'Esc' triggers cancelCellEditing KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); component.getInputMap(JComponent.WHEN_FOCUSED).put(esc, "cancelCellEditing"); component.getActionMap().put("cancelCellEditing", new AbstractAction() { public void actionPerformed(ActionEvent e) { cancelCellEditing(); } }); }
public LooseFocusAction( JComponent c ) { super(); this.c = c; c.getInputMap().put( KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ), "lost" ); c.getActionMap().put( "lost", this ); }