Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-13
4
Medium Priority
?
968 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
  • 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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

772 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