Solved

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

Posted on 2009-07-13
4
945 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

920 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

16 Experts available now in Live!

Get 1:1 Help Now