Java JDialog Escape Key addEscapeKeyCloseAction(final JDialog dialog)

Here you can find the source of addEscapeKeyCloseAction(final JDialog dialog)

Description

Adds a key listener to a given JDialog that diposes it when the escape key is pressed.

License

Open Source License

Declaration

public static void addEscapeKeyCloseAction(final JDialog dialog) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.event.*;

import javax.swing.*;

public class Main {
    /**/*from   w w  w  . j  a  v  a 2s .co m*/
     * Adds a key listener to a given {@link JDialog} that diposes it when the escape
     * key is pressed.
     */
    public static void addEscapeKeyCloseAction(final JDialog dialog) {
        dialog.getRootPane().registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
    }

    /**
     * Schedule disposal of a frame (async).
     */
    public static void dispose(final JFrame frame) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                if (frame.isDisplayable())
                    frame.dispose();
            }
        });
    }
}

Related

  1. addCancelEscape(JDialog f, AbstractAction cancelAction)
  2. addDisposeActionWithEscapeKey(final JDialog dialog)
  3. addDisposeOnAction(AbstractButton which, final JDialog dia)
  4. addDisposeOnEscape(final JDialog dia)
  5. addEscapeExitListeners(final JDialog window)
  6. addEscapeKeyCloseAction(final JDialog dialog)
  7. addEscapeListener(final JDialog dialog)
  8. addEscapeListener(final JDialog dialog)
  9. addEscapeListener(final JDialog dialog)