Java JDialog Key Event setupOKCancelHotkeys(JDialog dlg, JButton btnOK, final JButton btnCancel)

Here you can find the source of setupOKCancelHotkeys(JDialog dlg, JButton btnOK, final JButton btnCancel)

Description

set action for a dialog when the Esc or Enter keys are pressed - map Esc to Cancel button and Enter to OK button.

License

Mozilla Public License

Parameter

Parameter Description
dlg the dialog to set
btnOK the OK button
btnCancel the Cancel button

Declaration

public static void setupOKCancelHotkeys(JDialog dlg, JButton btnOK,
        final JButton btnCancel) 

Method Source Code

//package com.java2s;
/*//  w w w . j  a v  a 2 s. c  om
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 *  except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is MozillaTranslator (Mozilla Localization Tool)
 *
 * The Initial Developer of the Original Code is Henrik Lynggaard Hansen
 *
 * Portions created by Henrik Lynggard Hansen are
 * Copyright (C) Henrik Lynggaard Hansen.
 * All Rights Reserved.
 *
 * Contributor(s):
 * Henrik Lynggaard Hansen (Initial Code)
 * Tsahi Asher <tsahi_75@yahoo.com>
 *
 */

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class Main {
    /**
     *set action for a dialog when the Esc or Enter keys are pressed - map Esc
     *to Cancel button and Enter to OK button.
     *@param dlg the dialog to set
     *@param btnOK the OK button
     *@param btnCancel the Cancel button
     */
    public static void setupOKCancelHotkeys(JDialog dlg, JButton btnOK,
            final JButton btnCancel) {
        dlg.getRootPane().setDefaultButton(btnOK);
        String actionName = "Close";
        Action closeAction = new AbstractAction(actionName) {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnCancel.doClick();
            }
        };

        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JLayeredPane layeredPane = dlg.getLayeredPane();
        layeredPane.getActionMap().put(actionName, closeAction);

        layeredPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                stroke, actionName);
    }
}

Related

  1. installCloseKeyBinding(final JDialog dialog)
  2. installEnterKey(final JDialog comp, final Action action)
  3. loadCommonKeyMap(final JDialog dialog)
  4. registerCloseAction(final JDialog dialog, KeyStroke keyStroke)