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

voiddecorate(final JDialog d, boolean closeOnEscape)
decorate
center(d);
setIcon(d);
if (closeOnEscape) {
    addEscapeListener(d);
voidenableCloseByEscape(final JDialog dialog)
enable Close By Escape
AbstractAction closeAction = new AbstractAction() {
    public void actionPerformed(ActionEvent actionEvent) {
        dialog.setVisible(false);
        dialog.dispose();
};
KeyStroke escapeStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
dialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(escapeStroke,
...
voidinstallEscapeCloseOperation(final JDialog dialog)
Add a keyboard shortcut to allow ESC to close the dialog.
JRootPane root = dialog.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeStroke, dispatchWindowClosingActionMapKey);
Action dispatchClosing = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent event) {
        dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
};
...
voidinstallEscapeCloseOperation(final JDialog dialog)
install Escape Close Operation
Action dispatchClosing = new AbstractAction() {
    private static final long serialVersionUID = 1L;
    @Override
    public void actionPerformed(final ActionEvent event) {
        dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
};
JRootPane root = dialog.getRootPane();
...
voidinstallEscapeCloseOperation(final JDialog dialog)
install Escape Close Operation
Action dispatchClosing = new AbstractAction() {
    public void actionPerformed(ActionEvent event) {
        dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
};
JRootPane root = dialog.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESCAPE_KEY_STROKE, ESCAPE_KEY);
root.getActionMap().put(ESCAPE_KEY, dispatchClosing);
...
voidinstallEscapeKey(final JDialog comp)
install Escape Key
KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
Action escapeAction = new AbstractAction() {
    public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
        comp.setVisible(false);
};
comp.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE"); 
comp.getRootPane().getActionMap().put("ESCAPE", escapeAction); 
...
voidregisterEscapeKey(final JDialog dialog, ActionListener actionListener)
register Escape Key
dialog.getRootPane().registerKeyboardAction(actionListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
        JComponent.WHEN_IN_FOCUSED_WINDOW);
voidsetEscapeAction(JDialog dialog, Action action)
Associate a custom action to be called when the Esc key is pressed.
setEscapeAction(dialog.getRootPane(), action);
voidsetEscapeClosable(JDialog dialog)
Make a dialog closeable by pressing the Esc key.
setEscapeAction(dialog.getRootPane(), makeCloseAction(dialog));