Java JFrame addEscapeExitListeners(final JFrame window)

Here you can find the source of addEscapeExitListeners(final JFrame window)

Description

add Escape Exit Listeners

License

Open Source License

Declaration

public static void addEscapeExitListeners(final JFrame window) 

Method Source Code


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

import java.awt.Component;
import java.awt.Container;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class Main {
    public static void addEscapeExitListeners(final JFrame window) {
        addKeyAdapterRecursively(window, new KeyAdapter() {
            @Override//from w ww.j  ava2  s . c  o m
            public void keyPressed(final KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    System.exit(0);
                }
            }
        });
    }

    public static void addEscapeExitListeners(final JDialog window) {
        addKeyAdapterRecursively(window, new KeyAdapter() {
            @Override
            public void keyPressed(final KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    System.exit(0);
                }
            }
        });
    }

    public static void addKeyAdapterRecursively(final Container container, final KeyAdapter keyAdapter) {
        container.addKeyListener(keyAdapter);
        for (int i = 0; i < container.getComponentCount(); i++) {
            final Component child = container.getComponent(i);
            if (child instanceof Container) {
                addKeyAdapterRecursively((Container) child, keyAdapter);
            }
            child.addKeyListener(keyAdapter);
        }
    }
}

Related

  1. abrirJFrameCentralizado(JFrame dialog)
  2. activateWindowClosingButton(JFrame frame)
  3. addDisposeActionWithEscapeKey(final JFrame frame)
  4. addEscapeListener(final JFrame frame)
  5. addJFrameReporter(JFrame to)
  6. addPanelToFrame(final JFrame frame, final JPanel panel, final String title)
  7. addProgressBar(final JFrame win)