Java JDialog Escape Key actionOnEsc(final JDialog dialog, final Action action)

Here you can find the source of actionOnEsc(final JDialog dialog, final Action action)

Description

action On Esc

License

Open Source License

Declaration

public static void actionOnEsc(final JDialog dialog, final Action action) 

Method Source Code


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

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

public class Main {
    public static void actionOnEsc(final JDialog dialog, final Action action) {
        KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                action.actionPerformed(null);
            }/*  w  w w  .  j ava2  s  .  c  om*/
        };

        dialog.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
        dialog.getRootPane().getActionMap().put("ESCAPE", escapeAction);
    }
}

Related

  1. addCancelByEscapeKey(JDialog fDialog, AbstractAction cancelAction)
  2. addCancelEscape(JDialog f, AbstractAction cancelAction)
  3. addDisposeActionWithEscapeKey(final JDialog dialog)
  4. addDisposeOnAction(AbstractButton which, final JDialog dia)