Java JDialog Escape Key installEscapeKey(final JDialog comp)

Here you can find the source of installEscapeKey(final JDialog comp)

Description

install Escape Key

License

Open Source License

Declaration

public static void installEscapeKey(final JDialog comp) 

Method Source Code

//package com.java2s;
/*//from   w  w w  .  ja  v  a2s .com
 * Copyright (C) 2005 G?rard Milmeister
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class Main {
    public static void installEscapeKey(final JDialog comp) {
        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"); //$NON-NLS-1$
        comp.getRootPane().getActionMap().put("ESCAPE", escapeAction); //$NON-NLS-1$
    }
}

Related

  1. decorate(final JDialog d, boolean closeOnEscape)
  2. enableCloseByEscape(final JDialog dialog)
  3. installEscapeCloseOperation(final JDialog dialog)
  4. installEscapeCloseOperation(final JDialog dialog)
  5. installEscapeCloseOperation(final JDialog dialog)
  6. registerEscapeKey(final JDialog dialog, ActionListener actionListener)
  7. setEscapeAction(JDialog dialog, Action action)
  8. setEscapeClosable(JDialog dialog)