Here you can find the source of makeJDialogCancellable(final Window w, final Action cancelAction, final boolean disposeOnCancel)
Parameter | Description |
---|---|
w | The Window which you want to make cancelable with the ESC key. Must be either a JFrame or a JDialog. |
cancelAction | The action to invoke on cancelation, or null for nothing |
disposeOnCancel | If true, the window will be disposed after invoking the provided action when the ESC key is pressed. Otherwise, the provided action will be invoked, but the window won't be closed. If you set this to false, and don't provide an action, nothing interesting will happen when ESC is pressed in your dialog. |
public static void makeJDialogCancellable(final Window w, final Action cancelAction, final boolean disposeOnCancel)
//package com.java2s; //License from project: Open Source License import java.awt.Window; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.ActionMap; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.KeyStroke; public class Main { /**// ww w . j a va2 s .co m * Arrange for an existing JDialog or JFrame to close nicely when the ESC * key is pressed. Called with an Action, which will become the cancelAction * of the dialog. * <p> * Note: we explicitly close the dialog from this code. * * @param w The Window which you want to make cancelable with the ESC key. Must * be either a JFrame or a JDialog. * @param cancelAction The action to invoke on cancelation, or null for nothing * @param disposeOnCancel If true, the window will be disposed after invoking the provided * action when the ESC key is pressed. Otherwise, the provided action will be invoked, * but the window won't be closed. If you set this to false, and don't provide an action, * nothing interesting will happen when ESC is pressed in your dialog. */ public static void makeJDialogCancellable(final Window w, final Action cancelAction, final boolean disposeOnCancel) { JComponent c; if (w instanceof JFrame) { c = (JComponent) ((JFrame) w).getRootPane(); } else if (w instanceof JDialog) { c = (JComponent) ((JDialog) w).getRootPane(); } else { throw new IllegalArgumentException( "The window argument has to be either a JFrame or JDialog." + //$NON-NLS-1$ " You provided a " + (w == null ? null : w.getClass().getName())); //$NON-NLS-1$ } InputMap inputMap = c .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); ActionMap actionMap = c.getActionMap(); inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), "cancel"); //$NON-NLS-1$ //$NON-NLS-2$ actionMap.put("cancel", new AbstractAction() { //$NON-NLS-1$ public void actionPerformed(ActionEvent e) { if (cancelAction != null) { cancelAction.actionPerformed(e); } if (disposeOnCancel) { w.dispose(); } } }); } /** * Works like {@link #makeJDialogCancellable(Window, Action, boolean)} * with disposeOnCancel set to true. * * @param w The Window to attach the ESC event handler to * @param cancelAction The action to perform. null is allowed: no custom * action will be performed, but the dialog will still be disposed on ESC. */ public static void makeJDialogCancellable(final Window w, final Action cancelAction) { makeJDialogCancellable(w, cancelAction, true); } }