Close a JFrame with the ESC key

  • Thread starter Allan Valeriano
  • Start date
A

Allan Valeriano

Hi,


I have this class that extends JFrame, and I'd like to close it when
the user presses the ESC key.
I've tryed the code:

addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 27){
System.out.println("closing...");
close();
}
}
public void keyTyped(KeyEvent e) {
}
});

but it seems useless, since there's no output when the ESC key is
pressed.
Anybody knows how to do that?


thanks in advance
Allan Valeriano
 
J

Jason Cavett

Hi,

I have this class that extends JFrame, and I'd like to close it when
the user presses the ESC key.
I've tryed the code:

addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 27){
System.out.println("closing...");
close();
}
}
public void keyTyped(KeyEvent e) {
}
});

but it seems useless, since there's no output when the ESC key is
pressed.
Anybody knows how to do that?

thanks in advance
Allan Valeriano

While I don't know if this is your problem, you should use
KeyEvent.VK_ESCAPE instead of 27. IIRC, 27 will not necessarily
always map to the same value on a keyboard if the keyboard is for a
different language.

Also, within your code, what object are you adding that key event to?
For example, assuming you are implementing that method inside the
JFrame in question, you should have...

this.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 27){
System.out.println("closing...");
close();
}
}
public void keyTyped(KeyEvent e) {
}
});

(Notie the "this" reference on the first line.)

Just wanted to clarify to weed out any minor issues first.
 
T

Tom Hawtin

Allan said:
I have this class that extends JFrame, and I'd like to close it when
the user presses the ESC key.
addKeyListener(new KeyListener() {

It's a really poor idea to needlessly extend classes. I know there is
lots of bad tutorial code out there that inappropriately inherit, but it
is really quite poor design.
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {

keyTyped would be more conventional.
if (e.getKeyCode() == 27){
but it seems useless, since there's no output when the ESC key is
pressed.

Do any other keys call that method?

Key events are fired for a single component. So if the focus is in a
text field, the enclosing frame will not receive the event (nor would an
enclosing JComboBox, incidentally).

The magic parts of the APIs are:

JComponent.getInputMap(WHEN_IN_FOCUSED_WINDOW)
JComponent.getActionMap()

(If you want to see how it's used, look at the docs for
JComponent.registerKeyboardAction(ActionListener,String,KeyStroke,int),
which was the old, easier-to-use way of doing it.)

Tom Hawtin
 
B

blmblm

It's a really poor idea to needlessly extend classes. I know there is
lots of bad tutorial code out there that inappropriately inherit, but it
is really quite poor design.

Your objection is to extending JFrame, right? I ask because the
placement of your comment made me think initially that you were
objecting to the anonymous inner class, which seems completely
reasonable and even idiomatic. Just checking!
keyTyped would be more conventional.

But would it work here? My understanding is that KeyTyped events
are generated only for Unicode characters ....

I started to type "but I'm too slack to try it", but really, how
long would it take to write a simple test program.

My test program suggests that keyTyped is invoked when the
ESC key is pressed (huh!), but the character you get (result of
calling getKeyChar on the event passed to keyTyped) appears to be
KeyEvent.CHAR_UNDEFINED. And the API says that calling getKeyCode
in keyTyped isn't guaranteed to produce meaningful results. So this
may not be the way to go.

I agree, by the way, that the OP's problem may be with which component
has the keyboard focus.
 
A

Allan Valeriano

Hi,

I have this class that extends JFrame, and I'd like to close it when
the user presses the ESC key.
I've tryed the code:

addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 27){
System.out.println("closing...");
close();
}
}
public void keyTyped(KeyEvent e) {
}
});

but it seems useless, since there's no output when the ESC key is
pressed.
Anybody knows how to do that?

thanks in advance
Allan Valeriano


Nevermind.
I found this solution, and it's working fine. Thanks anyway.

KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,
0, false);
Action escapeAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//close the frame
}
};
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke,
"ESCAPE");
getRootPane().getActionMap().put("ESCAPE", escapeAction);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top