Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to close a JDialog/ modal window when user press esc key

Posted on 2009-07-13
4
Medium Priority
?
965 Views
Last Modified: 2013-11-23
I have a JDialog window with a Ok button. Along with clicking on the button or the close icon, i want to close the modal window when user press esc key. Please tell me how can i do this. I have add a KeyListener with the JDialog but only the button is focused.
0
Comment
Question by:pdidominic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Assisted Solution

by:Thomas4019
Thomas4019 earned 400 total points
ID: 24842346
You could add an KeyListener to the Dialog and listen for the "esc" key.

http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 24842525
You need something like:

            dialog = new JDialog(this);
            dialog.addKeyListener(new CloseAction());
......
 
    private class CloseAction extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                dialog.dispose();
            }
        }
    }   

Open in new window

0
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 24844650
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24846971
pdidominic, can you show how you employed the accepted answer with the JDialog?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question