Java Utililty Methods JDialog Escape Key

List of utility methods to do JDialog Escape Key

Description

The list of methods to do JDialog Escape Key are organized into topic(s).

Method

voidactionOnEsc(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);
...
voidaddCancelByEscapeKey(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);
voidaddCancelEscape(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);
voidaddDisposeActionWithEscapeKey(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();
};
...
voidaddDisposeOnAction(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();
});
voidaddDisposeOnEscape(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();
});
...
voidaddEscapeExitListeners(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);
});
...
voidaddEscapeKeyCloseAction(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);
voidaddEscapeKeyCloseAction(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);
voidaddEscapeListener(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);