detecting escape hit on a window - Java

Status
Not open for further replies.

Nimdok

Ars Scholae Palatinae
1,214
I'm writing a set of dialogs, and I'd like the user to be able to press escape to close them. I'm aware of the KeyListener interface, however that is only good for a single component, not the entire window.

What do I need to do to listen for all window events? I searched ars and java.sun.com, and dug through the Window and Frame documentation, but I cannot find anything.
 

gaev

Wise, Aged Ars Veteran
145
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class TestDialog extends JDialog implements KeyListener
{

public TestDialog()
{
setContentPane(new MyContentPane(this));
}

public static void main(String[] args)
{
TestDialog jd = new TestDialog();
JButton b = new JButton("I do nothing!");
JTextField tf = new JTextField(10);
jd.getContentPane().setLayout(new FlowLayout());
jd.setSize(300, 300);
jd.getContentPane().add(tf);
jd.getContentPane().add(b);
jd.setVisible(true);

jd.addKeyListener(jd);
}


public void keyPressed(KeyEvent e)
{
System.out.println("Key Pressed");
}

public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
System.out.println("We have a winner!!!");
setVisible(false);
}
System.out.println("Key Released");
}

public void keyTyped(KeyEvent e)
{
System.out.println("Key Typed");
}

public class MyContentPane extends JPanel
{
TestDialog dlg;
public MyContentPane(TestDialog dlg)
{
this.dlg = dlg;
}

public Component add(Component comp)
{
comp.addKeyListener(dlg);
return super.add(comp);
}

public Component add(Component comp, int index)
{
comp.addKeyListener(dlg);
return super.add(comp, index);
}

public void add(Component comp, Object constraints)
{
comp.addKeyListener(dlg);
super.add(comp, constraints);
}

public void add(Component comp, Object constraints, int index)
{
comp.addKeyListener(dlg);
super.add(comp, constraints, index);
}

public Component add(String name, Component comp)
{
comp.addKeyListener(dlg);
return super.add(name, comp);
}
}

}

What it is is a modified JDialog which also doubles as a KeyListener.
This allows for me to add a KeyListener to every component that is added to the JDialog. This way the keys can be captured no matter what object has the focus in the dialog. You can use something like this in your program.

-gaev
 

gaev

Wise, Aged Ars Veteran
145
Well, here's the deal with java. Sun has to make everything so that it fits neatly into the java framework, for consistency. In java you can add a keylistener to just the JDialog. This works great... until you start adding components to the dialog. Then all the other components in the dialog start eating up all the events before they are ever passed down to the JDialog. And that is usually good, because you don't want every object to be notified of an event that has already been handled. However, in your case you want to catch that escape key, regardless of which object is receiving the events. The only way to make that real consistant in java is to explicitly tell each object that it has to deal with that escape key. It gives you more flexibility that way. Say, instead of catching the escape key you wanted to catch the enter key, but you have a text box inside the dialog that you want to be able to hold multiple lines. You could not add the KeyListener to that particular object so you could keep the functionality that you want. Overall it may be a pain, but I think that is the only way to do this (at least in the most recent version of java).
 
Status
Not open for further replies.