Java JDialog Escape Key addEscapeExitListeners(final JDialog window)

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

Description

add Escape Exit Listeners

License

Open Source License

Declaration

public static void addEscapeExitListeners(final JDialog 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 w  w. j a  va 2  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. addCancelByEscapeKey(JDialog fDialog, AbstractAction cancelAction)
  2. addCancelEscape(JDialog f, AbstractAction cancelAction)
  3. addDisposeActionWithEscapeKey(final JDialog dialog)
  4. addDisposeOnAction(AbstractButton which, final JDialog dia)
  5. addDisposeOnEscape(final JDialog dia)
  6. addEscapeKeyCloseAction(final JDialog dialog)
  7. addEscapeKeyCloseAction(final JDialog dialog)
  8. addEscapeListener(final JDialog dialog)
  9. addEscapeListener(final JDialog dialog)