Java JDialog Escape Key installEscapeCloseOperation(final JDialog dialog)

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

Description

install Escape Close Operation

License

Open Source License

Declaration

public static void installEscapeCloseOperation(final JDialog 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 www  . j  a va 2 s  . 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. closeOnEsc(JDialog dialog)
  2. closeOnEscape(final JDialog dialog)
  3. closeOnEscapePressed(final JDialog dialog)
  4. decorate(final JDialog d, boolean closeOnEscape)
  5. enableCloseByEscape(final JDialog dialog)
  6. installEscapeCloseOperation(final JDialog dialog)
  7. installEscapeCloseOperation(final JDialog dialog)
  8. installEscapeKey(final JDialog comp)
  9. registerEscapeKey(final JDialog dialog, ActionListener actionListener)