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;
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.JRootPane;
import javax.swing.KeyStroke;

public class Main {
    private static final KeyStroke ESCAPE_STROKE = KeyStroke.getKeyStroke(
            KeyEvent.VK_ESCAPE, 0);
    private static final String DISPATCH_WINDOW_CLOSING_ACTION_MAP_KEY = "com.spodding.tackline.dispatch:WINDOW_CLOSING";

    public static void installEscapeCloseOperation(final JDialog dialog) {
        Action dispatchClosing = new AbstractAction() {
            private static final long serialVersionUID = 1L;

            @Override/*from w  ww.j  a v  a  2 s.  c  o m*/
            public void actionPerformed(final ActionEvent event) {
                dialog.dispatchEvent(new WindowEvent(dialog,
                        WindowEvent.WINDOW_CLOSING));
            }
        };
        JRootPane root = dialog.getRootPane();
        root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                ESCAPE_STROKE, DISPATCH_WINDOW_CLOSING_ACTION_MAP_KEY);
        root.getActionMap().put(DISPATCH_WINDOW_CLOSING_ACTION_MAP_KEY,
                dispatchClosing);
    }
}

Related

  1. closeOnEscape(final JDialog dialog)
  2. closeOnEscapePressed(final JDialog dialog)
  3. decorate(final JDialog d, boolean closeOnEscape)
  4. enableCloseByEscape(final JDialog dialog)
  5. installEscapeCloseOperation(final JDialog dialog)
  6. installEscapeCloseOperation(final JDialog dialog)
  7. installEscapeKey(final JDialog comp)
  8. registerEscapeKey(final JDialog dialog, ActionListener actionListener)
  9. setEscapeAction(JDialog dialog, Action action)