Here you can find the source of closeOnEscapePressed(final JDialog dialog)
Parameter | Description |
---|---|
dialog | a parameter |
public static void closeOnEscapePressed(final JDialog dialog)
//package com.java2s; //License from project: Open Source License import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import javax.swing.AbstractAction; import javax.swing.ActionMap; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JRootPane; import javax.swing.KeyStroke; public class Main { /**/*from w w w . j a v a2s .c o m*/ * Registers an Action on the dialog on the Esc key that disposes the dialog. * * @param dialog */ public static void closeOnEscapePressed(final JDialog dialog) { JRootPane rootPane = dialog.getRootPane(); InputMap im = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap actionMap = rootPane.getActionMap(); KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); im.put(ks, "UiUtilsEscape"); actionMap.put("UiUtilsEscape", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { dialog.dispose(); } }); } }