Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 48
login form jsp example 2 26
CSV file parsing thru Java 13 31
Chrome and Firefox Java 5 34
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

810 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