Java JFrame closeOnEscape(final JFrame frame)

Here you can find the source of closeOnEscape(final JFrame frame)

Description

close On Escape

License

Apache License

Declaration

public static void closeOnEscape(final JFrame frame) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.event.ActionEvent;

import javax.swing.*;

public class Main {
    public static void closeOnEscape(final JFrame frame) {
        KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
        Action actionListener = new AbstractAction() {
            public void actionPerformed(ActionEvent actionEvent) {
                frame.setVisible(false);
                frame.dispose();/*from w w  w  .j a va  2  s .co m*/
            }
        };
        JRootPane rootPane = frame.getRootPane();
        InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(stroke, "ESCAPE");
        rootPane.getActionMap().put("ESCAPE", actionListener);
    }
}

Related

  1. changeLookAndFeel(final String lookName, final JFrame frame)
  2. chooseFile(String directoryPath, String dialogTitle, String selectedFileName, boolean saveDialog, String[] extensions, JFrame parentFrame)
  3. ChooserDemo(JFrame jf, Component comp)
  4. closeFrame(JFrame frame, JProgressBar progressBar)
  5. closeOnEsc(JFrame frame)
  6. closeOnEscape(final JFrame frame)
  7. closeOnKeyStroke(JFrame frame, KeyStroke keyStroke)
  8. closeWindow(final JFrame frame)
  9. configToApplicationFrame(JFrame frm)