register Escape Key for JDialog - Java Swing

Java examples for Swing:JOptionPane

Description

register Escape Key for JDialog

Demo Code

/*//from w  ww .java2  s.  co  m
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */
//package com.java2s;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.KeyStroke;

public class Main {
    public static void registerEscapeKey(final JDialog dialog,
            ActionListener actionListener) {
        dialog.getRootPane().registerKeyboardAction(actionListener,
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_IN_FOCUSED_WINDOW);
    }
}

Related Tutorials