Example usage for javax.swing JFrame getWindowListeners

List of usage examples for javax.swing JFrame getWindowListeners

Introduction

In this page you can find the example usage for javax.swing JFrame getWindowListeners.

Prototype

public synchronized WindowListener[] getWindowListeners() 

Source Link

Document

Returns an array of all the window listeners registered on this window.

Usage

From source file:Main.java

/**
 * Attaches a key event listener to given component, disposing of the given window
 * upon pressing escape within the context.
 * /*from   w  w  w.  j a  v a 2s .com*/
 * @param context
 * @param button
 */
public static void simulateExitOnEscape(Component context, JFrame window) {
    context.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                for (WindowListener wl : window.getWindowListeners()) {
                    wl.windowClosing(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
                }

                if (window != null)
                    window.dispose();
            }
        }
    });
}