Java JFrame installEscapeCloseOperation(final JFrame dialog)

Here you can find the source of installEscapeCloseOperation(final JFrame dialog)

Description

install Escape Close Operation

License

Open Source License

Declaration

public static void installEscapeCloseOperation(final JFrame dialog) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JRootPane;

import javax.swing.KeyStroke;

public class Main {
    private static final KeyStroke ESCAPE_KEY_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    private static final String ESCAPE_KEY = "WorldGrower:WINDOW_CLOSING";

    public static void installEscapeCloseOperation(final JDialog dialog) {
        Action dispatchClosing = new AbstractAction() {
            public void actionPerformed(ActionEvent event) {
                dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
            }/*from  ww  w .ja va2s  . c o  m*/
        };
        JRootPane root = dialog.getRootPane();
        root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESCAPE_KEY_STROKE, ESCAPE_KEY);
        root.getActionMap().put(ESCAPE_KEY, dispatchClosing);
    }

    public static void installEscapeCloseOperation(final JFrame dialog) {
        Action dispatchClosing = new AbstractAction() {
            public void actionPerformed(ActionEvent event) {
                dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
            }
        };
        JRootPane root = dialog.getRootPane();
        root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESCAPE_KEY_STROKE, ESCAPE_KEY);
        root.getActionMap().put(ESCAPE_KEY, dispatchClosing);
    }
}

Related

  1. getUserInput(JFrame _frame, String arg1, String arg2, Object arg3)
  2. getViewMenu(JFrame mainFrame)
  3. giveFrameHalfScreen(final JFrame frame)
  4. igualarFormularios(JFrame destino, JFrame origen)
  5. inputFiles(JFrame argParent)
  6. internalizeFrame(JFrame frame)
  7. isAv(JFrame frame)
  8. isVisibleNotMinimized(JFrame frame)
  9. jointButton(JFrame frame, final JButton button)