The Artima Developer Community
Sponsored Link

Java Buzz Forum
Closing Swing Windows with the Escape Key

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Sam Dalton

Posts: 143
Nickname: samd
Registered: Jun, 2003

Sam Dalton is a Java Developer with ThoughtWorks in teh UK
Closing Swing Windows with the Escape Key Posted: May 25, 2004 9:32 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Sam Dalton.
Original Post: Closing Swing Windows with the Escape Key
Feed Title: import java.*;
Feed URL: http://www.samjdalton.com/pebble/rss.xml
Feed Description: Random, Infrequent Bloggings of a Techie
Latest Java Buzz Posts
Latest Java Buzz Posts by Sam Dalton
Latest Posts From import java.*;

Advertisement

The source of much user frustration with Swing is when windows and dialogs don't act in the way that they expect. Generally, a user expects to be able to press the Escape key to close a dialog window (in Windows). By default Swing JDialogs don't provide this behavior. However, this can easily be implemented by creating a simple subclass of the JDialog class. The code below will equip any subclass with this "close on escape" functionality.

public abstract class EscapableDialog extends JDialog {
	protected JRootPane createRootPane() {
		Action action = new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				hide();
			}
		};
		KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
		JRootPane rootPane = new JRootPane();
		rootPane.getActionMap().put(action,action);
		rootPane.getInputMap(JComponent.WHEN_FOCUSED).put(stroke, action);
		return rootPane;
	}

	public EscapableDialog() throws HeadlessException {
		super();
	}

	public EscapableDialog(Dialog arg0) throws HeadlessException {
		super(arg0);
	}

	public EscapableDialog(Dialog arg0, boolean arg1) throws HeadlessException {
		super(arg0, arg1);
	}

	public EscapableDialog(Dialog arg0, String arg1) throws HeadlessException {
		super(arg0, arg1);
	}

	public EscapableDialog(Dialog arg0, String arg1, boolean arg2)
			throws HeadlessException {
	       super(arg0, arg1, arg2);
	}

	public EscapableDialog(Dialog arg0, String arg1, boolean arg2,
			GraphicsConfiguration arg3) throws HeadlessException {
		super(arg0, arg1, arg2, arg3);
	}

	public EscapableDialog(Frame arg0) throws HeadlessException {
		super(arg0);
	}

	public EscapableDialog(Frame arg0, boolean arg1) throws HeadlessException {
		super(arg0, arg1);
	}

	public EscapableDialog(Frame arg0, String arg1) throws HeadlessException {
		super(arg0, arg1);
	}

	public EscapableDialog(Frame arg0, String arg1, boolean arg2)
			throws HeadlessException {
		super(arg0, arg1, arg2);
	}

	public EscapableDialog(Frame arg0, String arg1, boolean arg2,
			GraphicsConfiguration arg3) {
		super(arg0, arg1, arg2, arg3);
	}

Read: Closing Swing Windows with the Escape Key

Topic: Adrian Colyer is Blogging Previous Topic   Next Topic Topic: Simple workflow system now on CPAN

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use