Java JDialog Escape Key closeOnEsc(final JDialog dlg)

Here you can find the source of closeOnEsc(final JDialog dlg)

Description

Configure dialog to close when Esc is pressed.

License

Open Source License

Declaration

public static void closeOnEsc(final JDialog dlg) 

Method Source Code

//package com.java2s;
/* Copyright (C) 2013 Interactive Brokers LLC. All rights reserved.  This code is subject to the terms
 * and conditions of the IB API Non-Commercial License or the IB API Commercial License, as applicable. */

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JDialog;

import javax.swing.KeyStroke;

public class Main {
    /** Configure dialog to close when Esc is pressed. */
    public static void closeOnEsc(final JDialog dlg) {
        dlg.getRootPane().getActionMap().put("Cancel", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                dlg.dispose();/*ww  w  . j  a  va2  s . c  o m*/
            }
        });

        dlg.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
    }
}

Related

  1. addEscapeListener(final JDialog dialog)
  2. addEscapeListener(final JDialog dialog, final boolean hide)
  3. addEscapeToCloseSupport(final JDialog dialog)
  4. addEscapeToCloseSupport(final JDialog dialog, final boolean fadeOnClose)
  5. addEscKeyAction(javax.swing.JDialog dialog, javax.swing.Action action)
  6. closeOnEsc(JDialog dialog)
  7. closeOnEscape(final JDialog dialog)
  8. closeOnEscapePressed(final JDialog dialog)
  9. decorate(final JDialog d, boolean closeOnEscape)