Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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
            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);
        }
    }
}