Java JDialog Escape Key addCancelEscape(JDialog f, AbstractAction cancelAction)

Here you can find the source of addCancelEscape(JDialog f, AbstractAction cancelAction)

Description

Maps the escape key with the action given (ideally, it should probably close the dialog window).

License

Open Source License

Parameter

Parameter Description
f the dialog window on which the action should be applied
cancelAction the action to be performed in response to the Esc key

Declaration

public static void addCancelEscape(JDialog f, AbstractAction cancelAction) 

Method Source Code

//package com.java2s;
/**  Some routines useful with dialog windows.
    //from   w  w w  .  ja  va2  s . com
<pre>
    
This file is part of FidoCadJ.
    
FidoCadJ is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
FidoCadJ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with FidoCadJ.  If not, see <http://www.gnu.org/licenses/>.
    
Copyright 2007-2013 by Davide Bucci
</pre>
*/

import java.awt.event.*;

import javax.swing.*;

public class Main {
    /** Maps the escape key with the action given (ideally, it should probably
    close the dialog window). Example follows:
        
    <pre>
    // Here is an action in which the dialog is closed
        
    AbstractAction cancelAction = new AbstractAction ()
    {
    public void actionPerformed (ActionEvent e)
    {
        setVisible(false);
    }
    };
    dialogUtil.addCancelEscape (this, cancelAction);
    </pre>
    @param f the dialog window on which the action should be applied
    @param cancelAction the action to be performed in response to the Esc
    key
        
    */
    public static void addCancelEscape(JDialog f, AbstractAction cancelAction) {
        // Map the Esc key to the cancel action description in the dialog box's
        // input map.

        String CANCEL_ACTION_KEY = "CANCEL_ACTION_KEY";

        int noModifiers = 0;

        KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, noModifiers, false);

        InputMap inputMap = f.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        inputMap.put(escapeKey, CANCEL_ACTION_KEY);

        f.getRootPane().getActionMap().put(CANCEL_ACTION_KEY, cancelAction);
    }
}

Related

  1. actionOnEsc(final JDialog dialog, final Action action)
  2. addCancelByEscapeKey(JDialog fDialog, AbstractAction cancelAction)
  3. addDisposeActionWithEscapeKey(final JDialog dialog)
  4. addDisposeOnAction(AbstractButton which, final JDialog dia)
  5. addDisposeOnEscape(final JDialog dia)
  6. addEscapeExitListeners(final JDialog window)