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

voidaddEscapeListener(final JDialog dialog)
add Escape Listener
dialog.getRootPane().registerKeyboardAction(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        dialog.dispose();
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
voidaddEscapeListener(final JDialog dialog)
add Escape Listener
ActionListener escListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        dialog.setVisible(false);
};
dialog.getRootPane().registerKeyboardAction(escListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
        JComponent.WHEN_IN_FOCUSED_WINDOW);
...
voidaddEscapeListener(final JDialog dialog, final boolean hide)
Adds a listener to the dialog to close the dialog if the user presses the escape key.
ActionListener escListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (hide) {
            dialog.setVisible(false);
        } else {
            dialog.dispose();
};
dialog.getRootPane().registerKeyboardAction(escListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
        JComponent.WHEN_IN_FOCUSED_WINDOW);
voidaddEscapeToCloseSupport(final JDialog dialog)
Adds a glass layer to the dialog to intercept all key events.
LayerUI<Container> layerUI = new LayerUI<Container>() {
    private boolean closing = false;
    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        ((JLayer) c).setLayerEventMask(AWTEvent.KEY_EVENT_MASK);
    @Override
...
voidaddEscapeToCloseSupport(final JDialog dialog, final boolean fadeOnClose)
Adds a glass layer to the dialog to intercept all key events.
final LayerUI<Container> layerUI = new LayerUI<Container>() {
    private static final long serialVersionUID = -7197890707453641705L;
    private boolean closing = false;
    @Override
    public void eventDispatched(final AWTEvent e, final JLayer<? extends Container> l) {
        if (e instanceof KeyEvent && ((KeyEvent) e).getKeyCode() == KeyEvent.VK_ESCAPE) {
            if (this.closing) {
                return;
...
voidaddEscKeyAction(javax.swing.JDialog dialog, javax.swing.Action action)
add Esc Key Action
addEscKeyAction(dialog.getRootPane(), action);
voidcloseOnEsc(final JDialog dlg)
Configure dialog to close when Esc is pressed.
dlg.getRootPane().getActionMap().put("Cancel", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        dlg.dispose();
});
dlg.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
        .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
voidcloseOnEsc(JDialog dialog)
close On Esc
closeOnKeyStroke(dialog.getRootPane(), ESC);
voidcloseOnEscape(final JDialog dialog)
Make the dialog close when the used presses escape.
closeOnEscape(dialog, dialog.getRootPane());
voidcloseOnEscapePressed(final JDialog dialog)
Registers an Action on the dialog on the Esc key that disposes the dialog.
JRootPane rootPane = dialog.getRootPane();
InputMap im = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = rootPane.getActionMap();
KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
im.put(ks, "UiUtilsEscape");
actionMap.put("UiUtilsEscape", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
...