Java JDialog Escape Key addEscapeKeyCloseAction(final JDialog dialog)

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

Description

Adds to the dialog a key listener that makes the dialog invisible.

License

Open Source License

Parameter

Parameter Description
dialog a parameter

Declaration

public static void addEscapeKeyCloseAction(final JDialog dialog) 

Method Source Code

//package com.java2s;
/*//w w  w  .j  ava2 s.c  o m
 * Carrot2 project.
 *
 * Copyright (C) 2002-2006, Dawid Weiss, Stanis?aw Osi?ski.
 * Portions (C) Contributors listed in "carrot2.CONTRIBUTORS" file.
 * All rights reserved.
 *
 * Refer to the full license file "carrot2.LICENSE"
 * in the root folder of the repository checkout or at:
 * http://www.carrot2.org/carrot2.LICENSE
 */

import java.awt.event.*;

import javax.swing.*;

public class Main {
    /**
     * Adds to the dialog a key listener that makes the dialog invisible.
     * 
     * @param dialog
     */
    public static void addEscapeKeyCloseAction(final JDialog dialog) {
        dialog.getRootPane().registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
    }
}

Related

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