Action action = new AbstractAction("Do It") { ... }; // This is the component we will register the keyboard shortcut with. JPanel pnl = new JPanel(); // Create KeyStroke that will be used to invoke the action. KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK); // Register Action in component's ActionMap. pnl.getActionMap().put("Do It", action); // Now register KeyStroke used to fire the action. I am registering this with the // InputMap used when the component's parent window has focus. pnl.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "Do It");
protected JDialog createDialog(Component parent) { Frame frame = parent instanceof Frame ? (Frame) parent : (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent); JDialog dialog = new JDialog(frame, ("Select Font"), true); Action okAction = new DialogOKAction(dialog); Action cancelAction = new DialogCancelAction(dialog); JButton okButton = new JButton(okAction); okButton.setFont(DEFAULT_FONT); JButton cancelButton = new JButton(cancelAction); cancelButton.setFont(DEFAULT_FONT); JPanel buttonsPanel = new JPanel(); buttonsPanel.setLayout(new GridLayout(2, 1)); buttonsPanel.add(okButton); buttonsPanel.add(cancelButton); buttonsPanel.setBorder(BorderFactory.createEmptyBorder(25, 0, 10, 10)); ActionMap actionMap = buttonsPanel.getActionMap(); actionMap.put(cancelAction.getValue(Action.DEFAULT), cancelAction); actionMap.put(okAction.getValue(Action.DEFAULT), okAction); InputMap inputMap = buttonsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), cancelAction.getValue(Action.DEFAULT)); inputMap.put(KeyStroke.getKeyStroke("ENTER"), okAction.getValue(Action.DEFAULT)); JPanel dialogEastPanel = new JPanel(); dialogEastPanel.setLayout(new BorderLayout()); dialogEastPanel.add(buttonsPanel, BorderLayout.NORTH); dialog.getContentPane().add(this, BorderLayout.CENTER); dialog.getContentPane().add(dialogEastPanel, BorderLayout.EAST); dialog.pack(); dialog.setLocationRelativeTo(frame); return dialog; }
public class ClosableWindow extends JFrame { public void setUp() { JPanel mainPanel = createMainPanel(); int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); KeyStroke closeKey = KeyStroke.getKeyStroke(KeyEvent.VK_W, mask); mainPanel.getInputMap().put(closeKey, "closeWindow"); mainPanel.getActionMap().put("closeWindow", new AbstractAction("Close Window") { @Override public void actionPerformed(ActionEvent e) { setVisible(false); dispose(); } }); getContentPane().add(mainPanel); } }
mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(Platform.getKeyStroke(KeyEvent.VK_R), "Reload");
JPanel content = (JPanel) getContentPane(); // from JFrame InputMap inputMap = content.getInputMap(); inputMap.put(KeyStroke.getKeyStroke("SPACE"), "pressed"); content.getActionMap().put("pressed", new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { currentButton = ++currentButton % buttons.length; buttons[currentButton].doClick(); } });
JPanel panel = (JPanel)frame.getContentPane(); InputMap imap = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
JPanel panel = new JPanel(); InputMap im = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK), "printAction"); ActionMap am = panel.getActionMap(); am.put("printAction", printHelloAction); // add to JPanel ActionMap JButton button = new JButton(printHelloAction); // add to JButton button.setText("Print Hello");
/** * Configure the panel to call an action when ESC is pressed and the panel has focus */ protected void useEscToClose(Action closeAction) { // Add support for using ESC to close the panel panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "ESCAPE_KEY"); panel.getActionMap().put("ESCAPE_KEY", closeAction); }
private void addKeyBindingForNewCategory(JPanel panel, CreateCategoryAction createCategoryAction) { InputMap inMap = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); KeyStroke ctrlN = KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK, true); inMap.put(ctrlN, "ctrl-n"); panel.getActionMap().put("ctrl-n", createCategoryAction); }
public void keybinding(JPanel mainPanel){ mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A , 0),mapKey); mainPanel.getActionMap().put(mapKey, new keyAction()); }
JPanel wrapperPanel = new JPanel(); ... InputMap wrapperPanelInputMap = wrapperPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); wrapperPanelInputMap.put(shiftKeyStroke, "pressedAction"); wrapperPanelInputMap.put(shiftReleasedKeyStroke, "releasedAction"); ... wrapperPanel.getActionMap().put("pressedAction", pressedAction); wrapperPanel.getActionMap().put("releasedAction", releasedAction);
/** * Adds a shortcut to hide the window when escape is pressed. * * @param panel The panel to add the shortcut to. */ private void hideWithEscape(final JPanel panel) { final KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); final Action escapeAction = new AbstractAction() { @Override public void actionPerformed(final ActionEvent e) { close(); } }; panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(escapeKeyStroke, "ESCAPE"); panel.getActionMap().put("ESCAPE", escapeAction); }
/** * Creates new form Sample */ public Sample() { initComponents(); pnlDraw.getActionMap().put(DeleteAction.class.getSimpleName(), deleteAction); pnlDraw.getInputMap().put((KeyStroke) deleteAction.getValue(Action.ACCELERATOR_KEY), DeleteAction.class.getSimpleName()); pnlDraw.getActionMap().put(RebuildAction.class.getSimpleName(), rebuildAction); pnlDraw.getInputMap().put((KeyStroke) rebuildAction.getValue(Action.ACCELERATOR_KEY), RebuildAction.class.getSimpleName()); }
private void setKeyBindings(final JPanel panel) { final ActionMap actionMap = panel.getActionMap(); final InputMap inputMap = panel.getInputMap(JComponent.WHEN_FOCUSED); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), KeyAction.VK_SPACE); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), KeyAction.VK_DELETE); actionMap.put(KeyAction.VK_SPACE, new KeyAction(KeyAction.VK_SPACE)); actionMap.put(KeyAction.VK_DELETE, new KeyAction(KeyAction.VK_DELETE)); }
JPanel panel = new JPanel(); panel.setLayout( new TableLayout( ... ) ); Action someAction = new AbstractAction( "GO" ) { public void actionPerformed() { } }; InputMap input = panel.getInputMap( JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); input.put( KeyStroke.getKeyStroke( "enter", "submit" ); panel.getActionMap().put("submit", someAction ); panel.add( button = new JButton( someAction ) ); panel.add( textField = new JTextField( ) );
JPanel panel = (JPanel)frame.getContentPane(); InputMap imap = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); imap.put(KeyStroke.getKeyStroke("LEFT"), "leftAction"); ActionMap amap = panel.getActionMap(); Action leftAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { doSomethingWhenLeftIsPressed(); } }; amap.put("leftAction", leftAction);
JFrame frame = new JFrame(); //e.g. JPanel content = (JPanel)frame.getContentPane(); content.getInputMap().put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER,0),"enterDown"); content.getActionMap().put("enterDown",new AbstractAction() { private static final long serialVersionUID = 1l; @Override public void actionPerformed(ActionEvent e) { //This action is called, as the Enter-key was pressed. } });
private void setKeyBindings(final JPanel panel) { final ActionMap actionMap = panel.getActionMap(); final InputMap inputMap = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), KeyAction.VK_LEFT); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), KeyAction.VK_RIGHT); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), KeyAction.VK_UP); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), KeyAction.VK_DOWN); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), KeyAction.VK_ESCAPE); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), KeyAction.VK_ENTER); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), KeyAction.VK_SPACE); actionMap.put(KeyAction.VK_LEFT, new KeyAction(KeyAction.VK_LEFT)); actionMap.put(KeyAction.VK_RIGHT, new KeyAction(KeyAction.VK_RIGHT)); actionMap.put(KeyAction.VK_UP, new KeyAction(KeyAction.VK_UP)); actionMap.put(KeyAction.VK_DOWN, new KeyAction(KeyAction.VK_DOWN)); actionMap.put(KeyAction.VK_ESCAPE, new KeyAction(KeyAction.VK_ESCAPE)); actionMap.put(KeyAction.VK_ENTER, new KeyAction(KeyAction.VK_ENTER)); actionMap.put(KeyAction.VK_SPACE, new KeyAction(KeyAction.VK_SPACE)); }
/** * Installs actions and key bindings on the datePicker's linkPanel. Does * nothing if the linkPanel is null. * * PRE: keybindings installed on picker. */ protected void installLinkPanelKeyboardActions() { if (datePicker.getLinkPanel() == null) return; ActionMap map = datePicker.getLinkPanel().getActionMap(); map.put(JXDatePicker.HOME_COMMIT_KEY, datePicker.getActionMap().get( JXDatePicker.HOME_COMMIT_KEY)); map.put(JXDatePicker.HOME_NAVIGATE_KEY, datePicker.getActionMap().get( JXDatePicker.HOME_NAVIGATE_KEY)); InputMap inputMap = datePicker.getLinkPanel().getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW); // PENDING: get from LF inputMap.put(KeyStroke.getKeyStroke("F5"), JXDatePicker.HOME_COMMIT_KEY); inputMap.put(KeyStroke.getKeyStroke("shift F5"), JXDatePicker.HOME_NAVIGATE_KEY); }
/** * Create a PixelBoard. * * @param sheet the related sheet * @param selected true for pre-selected, false for collapsed */ public PixelBoard (Sheet sheet, boolean selected) { super(Board.PIXEL, sheet.getLocationService(), eventsRead, selected, false, false, false); // Needed to process user input when RETURN/ENTER is pressed getComponent().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put( KeyStroke.getKeyStroke("ENTER"), "ParamAction"); getComponent().getActionMap().put("ParamAction", new ParamAction()); defineLayout(); }