Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Java Tip 72: Press Escape to close your Swing dialog windows

Discover how to program keystroke responses in your Dialogs' parent classes

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
In Microsoft Windows environments (95, 98, NT), users can press the Escape key to close dialog windows. By default, with Java applications, this behavior is offered by neither AWT nor JFC/Swing dialog windows. To present users with a more common environment, you can add this behavior to your Java programs. In Java Tip 69, "Press Escape to close your Java dialog windows," you learned how to accommodate this behavior for regular AWT dialog windows. In this tip, you'll see how much easier it is to do the same thing with JFC/Swing dialog windows.

Since the JFC/Swing dialog window is a subclass of the AWT dialog window, theoretically you could simply change the class subclassed by the earlier EscapeDialog. Instead of subclassing the AWT dialog window, the EscapeDialog could subclass the JFC/Swing JDialog window. For all practical purposes, recursively adding key listeners would work, causing the Escape key to close the JFC/Swing dialog window.

While the change of parent classes from the Dialog to JDialog would work, the JFC/Swing component library offers a much simpler approach. The JComponent class defines a couple of registerKeyboardAction() methods for just this type of behavior:

public void registerKeyboardAction(ActionListener action, KeyStroke keyStroke, int condition);
public void registerKeyboardAction(ActionListener action, String command, KeyStroke keyStroke, int condition);


All you need to do is to create a custom subclass of JDialog, define an ActionListener for closing the JDialog, and register the Escape keystroke to call the action listener. Then, when the user presses the Escape key when the JDialog is open, JDialog automatically goes away.

Here's a closer look at those three steps:

Step 1. Subclass JDialog

public class EscapeDialog extends JDialog {
  ...
}


Step 2. Define the ActionListener

ActionListener actionListener = new ActionListener() {
  public void actionPerformed(ActionEvent actionEvent) {
     setVisible(false);
  }
};


Step 3. Define and register the keystroke

Since the registerKeyboardAction() method is part of the JComponent class definition, you must define the Escape keystroke and register the keyboard action with a JComponent, not with a JDialog. The JRootPane for the JDialog serves as an excellent choice to associate the registration, as this will always be visible. If you override the protected createRootPane() method of JDialog, you can return your custom JRootPane with the keystroke enabled:

protected JRootPane createRootPane() {
  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
  JRootPane rootPane = new JRootPane();
  rootPane.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
  return rootPane;
}


Technically speaking, that's all there is to it with the JFC/Swing JDialog window. You can then create and use the new-and-improved EscapeDialog window in all your JFC/Swing-based applications just as you would a JDialog. From a useability perspective, you may wish to replicate the many constructors of JDialog. The accompanying source code (EscapeDialog.java) provides a common set of nine constructors, as offered by the original JDialog class. In addition, the SetupEscapeFrame.java file demonstrates its usage.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (4)
Login
Forgot your account info?

About your Exellent helpBy Anonymous on January 24, 2010, 3:58 amyou have given nice help but we create jdialog by using GUI of netbeans then where do i place this code....and exit that dailog

Reply | Read entire comment

Exellent helpBy Anonymous on January 24, 2010, 12:06 amyou have given nice help but we created jdialog by using GUI of netbeans then where do i place this code....and exit that jdialog window .... regards tushar kshirsagar tusharit25@gmail.com

Reply | Read entire comment

Another ThanksBy Anonymous on June 9, 2009, 9:27 pmShort, sweet and to the point. Most of all though, incredibly useful tidbit of knowledge. Thanks for sharing dude!

Reply | Read entire comment

ThanksBy Anonymous on November 10, 2008, 12:18 amGood help

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • This Java Tip was excerpted from John Zukowski's Definitive Guide to Swing for Java 2, to be published by Apress! in summer 1999 http://www.apress.com/Catalog/CatalogMain.htm