Java examples for Swing:JDialog
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.
//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); } }