Java JFrame closeOnKeyStroke(JFrame frame, KeyStroke keyStroke)

Here you can find the source of closeOnKeyStroke(JFrame frame, KeyStroke keyStroke)

Description

close On Key Stroke

License

Open Source License

Declaration

public static void closeOnKeyStroke(JFrame frame, KeyStroke keyStroke) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Main {
    private static final Action CLOSE_ACTION = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            Window window = SwingUtilities.getWindowAncestor((Component) e.getSource());
            // Dispatch an event so it's as if the window's close button was clicked.
            // The client has to set up the right behavior for that case.
            window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
        }//from  ww  w . j  a  va 2 s  .  co m
    };

    public static void closeOnKeyStroke(JFrame frame, KeyStroke keyStroke) {
        closeOnKeyStroke(frame.getRootPane(), keyStroke);
    }

    private static void closeOnKeyStroke(JRootPane rootPane, KeyStroke keyStroke) {
        final String CLOSE_ACTION_NAME = "e.gui.JFrameUtilities.CloseFrameOnKeyStroke";
        rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, CLOSE_ACTION_NAME);
        rootPane.getActionMap().put(CLOSE_ACTION_NAME, CLOSE_ACTION);
    }
}

Related

  1. ChooserDemo(JFrame jf, Component comp)
  2. closeFrame(JFrame frame, JProgressBar progressBar)
  3. closeOnEsc(JFrame frame)
  4. closeOnEscape(final JFrame frame)
  5. closeOnEscape(final JFrame frame)
  6. closeWindow(final JFrame frame)
  7. configToApplicationFrame(JFrame frm)
  8. confirm(JFrame parent, String query, int type)
  9. createConfirmOnExitAdapter(final JFrame frame, final String title, final String message)