Java JDialog makeJDialogCancellable(final Window w, final Action cancelAction, final boolean disposeOnCancel)

Here you can find the source of makeJDialogCancellable(final Window w, final Action cancelAction, final boolean disposeOnCancel)

Description

Arrange for an existing JDialog or JFrame to close nicely when the ESC key is pressed.

License

Open Source License

Parameter

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.

Declaration

public static void makeJDialogCancellable(final Window w,
        final Action cancelAction, final boolean disposeOnCancel) 

Method Source Code

//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);
    }
}

Related

  1. getRootJDialog(Component component)
  2. getRootJDialog(java.awt.Component c)
  3. initialiseFrame(JDialog frame, JScrollPane viewport)
  4. isInsideScreen(JDialog component)
  5. jointButton(JDialog frame, final JButton button)
  6. messageDialog(String string, JDialog parentDialog)
  7. openDialog(final JDialog dialog)
  8. poseInsideScreen(JDialog component)
  9. positionDialogInContainer(JDialog dialog, Container frame, int horizontal, int vertical)