Java JRootPane setEscapeAction(JRootPane pane, Action action)

Here you can find the source of setEscapeAction(JRootPane pane, Action action)

Description

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

License

Open Source License

Parameter

Parameter Description
pane a parameter
action a parameter

Declaration

public static void setEscapeAction(JRootPane pane, 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 ww  w. j  a v a  2  s  .  c o  m*/
 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. installEscapeBinding(final Window window, final JRootPane rootPane, final boolean dispose)
  2. isSecondaryWindow(JRootPane rp)
  3. makeWindowLeopardStyle(JRootPane rootPane)
  4. registerEscapeAction(JRootPane pane, ActionListener l)
  5. setCancelAction(RootPaneContainer c, Action cancelAction)