List of utility methods to do JDialog Key Event
void | installCloseKeyBinding(final JDialog dialog) Install a close action binding to Ctrl-C /Command-C for the specified dialog.
Action close = new AbstractAction() { @Override public void actionPerformed(final ActionEvent event) { dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING)); }; int menuKeyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); KeyStroke closeStroke = KeyStroke.getKeyStroke(KeyEvent.VK_W, menuKeyMask); ... |
void | installEnterKey(final JDialog comp, final Action action) install Enter Key KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false); comp.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(enterKeyStroke, "ENTER"); comp.getRootPane().getActionMap().put("ENTER", action); |
void | loadCommonKeyMap(final JDialog dialog) This method enables exiting the dialog by pressing the escape key JRootPane rootPane = dialog.getRootPane(); rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), ACTION_KEY_ESCAPE); rootPane.getActionMap().put(ACTION_KEY_ESCAPE, new AbstractAction() { private static final long serialVersionUID = 0; public void actionPerformed(ActionEvent evt) { dialog.dispose(); }); |
void | registerCloseAction(final JDialog dialog, KeyStroke keyStroke) Registers a keystroke to close the given dialog. ActionListener escListener = new ActionListener() { public void actionPerformed(ActionEvent e) { dialog.dispose(); }; dialog.getRootPane().registerKeyboardAction(escListener, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
void | setupOKCancelHotkeys(JDialog dlg, JButton btnOK, final JButton btnCancel) set action for a dialog when the Esc or Enter keys are pressed - map Esc to Cancel button and Enter to OK button. dlg.getRootPane().setDefaultButton(btnOK); String actionName = "Close"; Action closeAction = new AbstractAction(actionName) { @Override public void actionPerformed(ActionEvent e) { btnCancel.doClick(); }; ... |