Java JFrame setEscapeClosable(JFrame frame)

Here you can find the source of setEscapeClosable(JFrame frame)

Description

Make a dialog closeable by pressing the Esc key.

License

Open Source License

Parameter

Parameter Description
frame a parameter

Declaration

public static void setEscapeClosable(JFrame frame) 

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.
    //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.Window;
import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
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);

    /**
     * Make a dialog closeable by pressing the Esc key.
     * {@link JDialog#dispose()} will be called.
     * 
     * @param dialog
     */
    public static void setEscapeClosable(JDialog dialog) {
        setEscapeAction(dialog.getRootPane(), makeCloseAction(dialog));
    }

    /**
     * Make a dialog closeable by pressing the Esc key.
     * {@link JFrame#dispose()} will be called.
     * 
     * @param frame
     */
    public static void setEscapeClosable(JFrame frame) {
        setEscapeAction(frame.getRootPane(), makeCloseAction(frame));
    }

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

    @SuppressWarnings("serial")
    public static Action makeCloseAction(final Window window) {
        return new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                closeWindowByEvent(window);
            }
        };
    }

    /**
     * Send a {@link WindowEvent#WINDOW_CLOSING} event to the supplied window.
     * This mimics closing by clicking the window close button.
     */
    public static void closeWindowByEvent(Window window) {
        window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
    }
}

Related

  1. setBlockAllUserInput(final JFrame frame, final boolean yesNo)
  2. setCursorFree(JFrame frame)
  3. setDialogLocation(JFrame frame, JFrame parentFrame)
  4. setDirty(JFrame frame, boolean isDirty)
  5. setEscapeAction(JFrame frame, Action action)
  6. setESCCloseable(final JFrame aFrame)
  7. setFrameBottomRight(final JFrame frame)
  8. setFramePositon(JFrame inTargetFrame)
  9. setFrameTitle(JFrame frame, File file)