List of utility methods to do JDialog Escape Key
void | actionOnEsc(final JDialog dialog, final Action action) action On Esc KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); Action escapeAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { action.actionPerformed(null); }; dialog.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE"); dialog.getRootPane().getActionMap().put("ESCAPE", escapeAction); ... |
void | addCancelByEscapeKey(JDialog fDialog, AbstractAction cancelAction) Force the escape key to call the same action as pressing the Cancel button. String CANCEL_ACTION_KEY = "CANCEL_ACTION_KEY"; int noModifiers = 0; KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, noModifiers, false); InputMap inputMap = fDialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(escapeKey, CANCEL_ACTION_KEY); fDialog.getRootPane().getActionMap().put(CANCEL_ACTION_KEY, cancelAction); |
void | addCancelEscape(JDialog f, AbstractAction cancelAction) Maps the escape key with the action given (ideally, it should probably close the dialog window). String CANCEL_ACTION_KEY = "CANCEL_ACTION_KEY"; int noModifiers = 0; KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, noModifiers, false); InputMap inputMap = f.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(escapeKey, CANCEL_ACTION_KEY); f.getRootPane().getActionMap().put(CANCEL_ACTION_KEY, cancelAction); |
void | addDisposeActionWithEscapeKey(final JDialog dialog) Adds the dispose action with escape key. KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); Action disposeAction = new AbstractAction() { private static final long serialVersionUID = 0L; @Override public void actionPerformed(ActionEvent e) { dialog.dispose(); }; ... |
void | addDisposeOnAction(AbstractButton which, final JDialog dia) Add an action listener which causes the associated dialog to be disposed. which.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dia.dispose(); }); |
void | addDisposeOnEscape(final JDialog dia) Add an action to the action map which disposes this dialog when ESCAPE is pressed. InputMap aof = dia.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); aof.put(KeyStroke.getKeyStroke("ESCAPE"), "dispose"); dia.getRootPane().getActionMap().put("dispose", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { dia.dispose(); }); ... |
void | addEscapeExitListeners(final JDialog window) add Escape Exit Listeners addKeyAdapterRecursively(window, new KeyAdapter() { @Override public void keyPressed(final KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { System.exit(0); }); ... |
void | addEscapeKeyCloseAction(final JDialog dialog) Adds a key listener to a given JDialog that diposes it when the escape key is pressed. dialog.getRootPane().registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent e) { dialog.dispose(); }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); |
void | addEscapeKeyCloseAction(final JDialog dialog) Adds to the dialog a key listener that makes the dialog invisible. dialog.getRootPane().registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent e) { dialog.setVisible(false); }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); |
void | addEscapeListener(final JDialog dialog) add Escape Listener KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
dialog.getRootPane().registerKeyboardAction(
e -> dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING)), stroke,
JComponent.WHEN_IN_FOCUSED_WINDOW);
|