Java Window closeByESC(final Window window, JPanel panel)

Here you can find the source of closeByESC(final Window window, JPanel panel)

Description

close By ESC

License

Apache License

Declaration

public static void closeByESC(final Window window, JPanel panel) 

Method Source Code

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

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JPanel;

import javax.swing.KeyStroke;

public class Main {

    public static void closeByESC(final Window window, JPanel panel) {
        panel.getActionMap().put("close", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                window.dispose();/*from w  w w.  j a va  2s . co  m*/
            }
        });

        panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
                .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");

    }
}

Related

  1. arrangeWithin(final Shape shapeToArrange, final Rectangle window, final int arrangement, Insets padding)
  2. buildWindow(Float opacity)
  3. cascade(Window[] windows, Rectangle dBounds, int separation)
  4. createWindow(String name, int width, int height, boolean guiOn)
  5. decorate(final Window w)
  6. dock(Window window, Window dockTo)
  7. enableCloseWindowWithEscape(final W window)