Solved

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

Posted on 2009-07-13
4
942 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 100 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 100 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 300 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now