Java JFrame registerCloseAction(final JFrame dialog, KeyStroke keyStroke)

Here you can find the source of registerCloseAction(final JFrame 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 JFrame dialog, KeyStroke keyStroke) 

Method Source Code

//package com.java2s;
/**//from  www.j a  v  a  2  s. 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. POPUP(JFrame frame, String message)
  2. popupError(JFrame parent, String title, String text)
  3. position(JFrame f)
  4. putOnWidestScreen(JFrame frame)
  5. refreshShape(final JFrame frame)
  6. registerMenuShortcut(String uniqueActionName, Action action, int keyCode, JFrame frame)
  7. renderDialog(JFrame theframe, String szMessage, int noffsetx, int noffsety)
  8. replaceGlassPane(JFrame frame, Component newGlassPane)
  9. restoreFrame(Class pClass, final JFrame pFrame, String pFrameId)