Decorates the given dialog with a action which will be executed when the ESC key is hit while the dialog or one of its subcomponents has the focus. - Java Swing

Java examples for Swing:JDialog

Description

Decorates the given dialog with a action which will be executed when the ESC key is hit while the dialog or one of its subcomponents has the focus.

Demo Code


//package com.java2s;

import java.awt.event.KeyEvent;

import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JDialog;

import javax.swing.KeyStroke;

public class Main {
    /**//from w ww  .java 2s. c  om
     * Decorates the given dialog with a action which will be executed when the
     * ESC key is hit while the dialog or one of its subcomponents has the
     * focus.
     * 
     * @param dialog
     *            the dialog to decorate.
     * @param action
     *            to execute when ESC was pressed.
     */
    public static void decorateWithActionOnESC(JDialog dialog, Action action) {
        dialog.getRootPane().registerKeyboardAction(action,
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_IN_FOCUSED_WINDOW);
    }
}

Related Tutorials