Java examples for Swing:JDialog
install Escape Close Operation for JDialog
//package com.java2s; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.WindowEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JRootPane; import javax.swing.KeyStroke; public class Main { private static final KeyStroke ESCAPE_KEY_STROKE = KeyStroke .getKeyStroke(KeyEvent.VK_ESCAPE, 0); private static final String ESCAPE_KEY = "WorldGrower:WINDOW_CLOSING"; public static void installEscapeCloseOperation(final JDialog dialog) { Action dispatchClosing = new AbstractAction() { public void actionPerformed(ActionEvent event) { dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING)); }//from w ww .j a v a2 s . c om }; JRootPane root = dialog.getRootPane(); root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( ESCAPE_KEY_STROKE, ESCAPE_KEY); root.getActionMap().put(ESCAPE_KEY, dispatchClosing); } public static void installEscapeCloseOperation(final JFrame dialog) { Action dispatchClosing = new AbstractAction() { public void actionPerformed(ActionEvent event) { dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING)); } }; JRootPane root = dialog.getRootPane(); root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( ESCAPE_KEY_STROKE, ESCAPE_KEY); root.getActionMap().put(ESCAPE_KEY, dispatchClosing); } }