Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

JavaTip 69: Press Escape to close your Java dialog windows

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

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 2

Here's the code for the componentAdded function:

     private void addKeyAndContainerListenerRecursively(Component c)
     {
//Add KeyListener to the Component passed as an argument
          c.addKeyListener(this);
//Check if the Component is a Container
          if(c instanceof Container) {
//Component c is a Container. The following cast is safe.
               Container cont = (Container)c;
//Add ContainerListener to the Container.
               cont.addContainerListener(this);
//Get the Container's array of children Components.
               Component[] children = cont.getComponents();
//For every child repeat the above operation.
               for(int i = 0; i < children.length; i++){
                    addKeyAndContainerListenerRecursively(children[i]);
               }
          }
     }


Additional actions for container components

To accomplish our task, we need to add the EscapeDialog as a KeyListener to the component passed as an argument (this is the newly added component if we're in the first recursion of the function). Then we check whether or not the component is a container. If it isn't, we're done, because the component doesn't contain any child components. If the component turns out to be a container, the container requires two additional actions:

  • Add EscapeDialog as a ContainerListener to the container, so the EscapeDialog receives notification if other components are added to the container in the future

  • Call this function recursively for every child of the container, so all components of the container receive EscapeDialog as a KeyListener


The keyPressed function

Because the EscapeDialog object is a KeyListener of all its descendent components, the function keyPressed will be called whenever a key is pressed and the focus belongs to the Dialog or one of its components:

     public void keyPressed(KeyEvent e)
     {
          int code = e.getKeyCode();
          if(code == KeyEvent.VK_ESCAPE){
//Key pressed is the Escape key. Hide this Dialog.
               setVisible(false);
          }
          else if(code == KeyEvent.VK_ENTER){
//Key pressed is the Enter key. Redefine performEnterAction() in subclasses 
to respond to pressing the Enter key.
               performEnterAction(e);
          }
//Insert code to process other keys here
     }


If the Escape key is pressed, we hide the dialog by calling function setVisible(false). In addition, you can program a response to any key pressed. For example, on pressing the Enter key, function performEnterAction is called. As it stands now, this function doesn't do anything, but you can redefine it in a subclass to do something useful.

In the constructor of the EscapeDialog we need to add this EscapeDialog to itself as a KeyListener and a ContainerListener:

     public EscapeDialog(Frame frame, String title, boolean modal)
     {
          super(frame, title, modal);
          addKeyAndContainerListenerRecursively(this);
     }


The source code for this article can be accessed at EscapeDialog.java.txt.

Conclusion

If you derive all your dialog boxes from EscapeDialog, the Escape key will automatically close them. Now you can concentrate on creating specific layouts and functionality for your dialogs without worrying about the basic functionality that today's users expect.

About the author

Eugene Podgorbunskikh is a senior developer at Logica Advantage kbs (http://www.akbs.com), where he works with Java and C++ to build customer-support applications.
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
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
  • The source code for this article http://www.javaworld.com/javatips/javatip69/EscapeDialog.java.txt
  • Event handling in JDK 1.1 http://www.geocities.com/Athens/7077/Java080.htm
  • The container-component relationship in Java http://mordor.cs.hut.fi/tik-76.278/group6/awtpat.html#container
  • An example of registering a Listener object on buttons added dynamically to a container, http://igwe4.vub.ac.be/javacursus/Java095.htm
  • Examples of recursive functions http://www.strath.ac.uk/CC/Courses/CCourse/subsection3_8_5.html