Java JFrame setEscapeAction(JFrame frame, Action action)

Here you can find the source of setEscapeAction(JFrame frame, Action action)

Description

Associate a custom action to be called when the Esc key is pressed.

License

Open Source License

Parameter

Parameter Description
frame a parameter
action a parameter

Declaration

public static void setEscapeAction(JFrame frame, Action action) 

Method Source Code

//package com.java2s;
/**************************************************************************
 OmegaT - Computer Assisted Translation (CAT) tool 
  with fuzzy matching, translation memory, keyword search, 
  glossaries, and translation leveraging into updated projects.
    //from w  w w.  j  av a  2s . c om
 Copyright (C) 2006 Henry Pijffers
       2013 Yu Tang
       2014-2015 Aaron Madlon-Kay
       Home page: http://www.omegat.org/
       Support center: http://groups.yahoo.com/group/OmegaT/
    
 This file is part of OmegaT.
    
 OmegaT 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.
    
 OmegaT 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************/

import java.awt.event.KeyEvent;

import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

public class Main {
    private static final KeyStroke ESC_KEYSTROKE = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);

    /**
     * Associate a custom action to be called when the Esc key is pressed.
     * 
     * @param dialog
     * @param action 
     */
    public static void setEscapeAction(JDialog dialog, Action action) {
        setEscapeAction(dialog.getRootPane(), action);
    }

    /**
     * Associate a custom action to be called when the Esc key is pressed.
     * 
     * @param frame
     * @param action 
     */
    public static void setEscapeAction(JFrame frame, Action action) {
        setEscapeAction(frame.getRootPane(), action);
    }

    /**
     * Associate a custom action to be called when the Esc key is pressed.
     * 
     * @param pane
     * @param action 
     */
    public static void setEscapeAction(JRootPane pane, Action action) {
        // Handle escape key to close the window
        pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESC_KEYSTROKE, "ESCAPE");
        pane.getActionMap().put("ESCAPE", action);
    }
}

Related

  1. saveMainWindowLocation(JFrame jfrm, String strPropertiesFilePath, String strPropertiesFileName)
  2. setBlockAllUserInput(final JFrame frame, final boolean yesNo)
  3. setCursorFree(JFrame frame)
  4. setDialogLocation(JFrame frame, JFrame parentFrame)
  5. setDirty(JFrame frame, boolean isDirty)
  6. setEscapeClosable(JFrame frame)
  7. setESCCloseable(final JFrame aFrame)
  8. setFrameBottomRight(final JFrame frame)
  9. setFramePositon(JFrame inTargetFrame)