Java JDialog Key Event registerCloseAction(final JDialog dialog, KeyStroke keyStroke)

Here you can find the source of registerCloseAction(final JDialog dialog, KeyStroke keyStroke)

Description

Registers a keystroke to close the given dialog.

License

Open Source License

Parameter

Parameter Description
dialog a parameter
keyStroke a parameter

Declaration

public static void registerCloseAction(final JDialog dialog, KeyStroke keyStroke) 

Method Source Code

//package com.java2s;
/**/*from  w  w  w  . ja va2s  . com*/
 * (c) 2000-2011 Carlos G?mez Rodr?guez, todos los derechos reservados / all rights reserved.
 * Licencia en license.txt / License in license.txt
 * File created: 26/10/2012 17:14:36
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class Main {
    /**
     * Registers a keystroke to close the given dialog.
     * @param dialog
     * @param keyStroke
     */
    public static void registerCloseAction(final JDialog dialog, KeyStroke keyStroke) {
        ActionListener escListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        };

        dialog.getRootPane().registerKeyboardAction(escListener, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
    }

    /**
     * Registers a keystroke to close the given dialog.
     * @param dialog
     * @param keyStroke
     */
    public static void registerCloseAction(final JFrame dialog, KeyStroke keyStroke) {
        ActionListener escListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        };

        dialog.getRootPane().registerKeyboardAction(escListener, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
    }
}

Related

  1. installCloseKeyBinding(final JDialog dialog)
  2. installEnterKey(final JDialog comp, final Action action)
  3. loadCommonKeyMap(final JDialog dialog)
  4. setupOKCancelHotkeys(JDialog dlg, JButton btnOK, final JButton btnCancel)