Java JFrame Parent closeFrameWhenEscapePressed(final JRootPane panel, final ActionListener actListener)

Here you can find the source of closeFrameWhenEscapePressed(final JRootPane panel, final ActionListener actListener)

Description

close Frame When Escape Pressed

License

Open Source License

Declaration

static void closeFrameWhenEscapePressed(final JRootPane panel, final ActionListener actListener) 

Method Source Code

//package com.java2s;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;

import javax.swing.JComponent;

import javax.swing.JRootPane;

import javax.swing.KeyStroke;

public class Main {
    public static final String ESCAPE_ACTION_NAME = "ESCAPE";

    static void closeFrameWhenEscapePressed(final JRootPane panel, final ActionListener actListener) {

        @SuppressWarnings("serial")
        Action escAction = new AbstractAction(ESCAPE_ACTION_NAME) {
            public void actionPerformed(ActionEvent evt) {
                actListener.actionPerformed(evt);
            }/*  w ww.  ja v  a 2 s . c  o m*/
        };
        escAction.putValue(Action.ACTION_COMMAND_KEY, ESCAPE_ACTION_NAME);

        KeyStroke escKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escKeyStroke, ESCAPE_ACTION_NAME);
        panel.getActionMap().put(ESCAPE_ACTION_NAME, escAction);
    }
}

Related

  1. AllPlatformGetFile(String dialogTitle, File locationIn, final String fileExtension, FileFilter nonMacFileFilter, boolean allowMultipleSelect, JLayeredPane parentFrame)
  2. AllPlatformSaveAs(Frame parentFrame, String dialogTitle, String directory, final String fileExtension, String fractionFileName, FileFilter nonMacFileFilter)
  3. AllPlatformSaveAs(Frame parentFrame, String dialogTitle, String directory, final String fileExtension, String fractionFileName, FileFilter nonMacFileFilter)
  4. buildParentFrame(Object inp)
  5. centreOverFrame(JInternalFrame win, JInternalFrame parent)
  6. createExceptionDialog(Frame parent, String title, Throwable error)
  7. createProgressDialog(Frame parentFrame, String title, JProgressBar progressBar)
  8. enableAllComponents(final boolean enable, final Frame parent)
  9. enableAllComponentsExcept(final boolean enable, final Frame parent, final Component... components)