in general the Esc key is used to hide the menu.. but in my case i have to show a menu on click of an Esc key. I have a combo I am doing the following
public class MyFrame extends JFrame implements KeyListener{
JPopupMenu menu = new JPopupMenu();
JTextField txt = new JTextField("TestField1");
JTextField txt1 = new JTextField("TestField2");
public MyFrame(){
init();
}
private void init(){
setLayout(new BorderLayout());
txt.addKeyListener(this);
add( txt,BorderLayout.WEST);
add(txt1,BorderLayout.CENTER);
pack();
setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("keypressed");
menu = new JPopupMenu();
menu.add("item1");
menu.add("item2");
menu.show(e.getComponent(),e.getComponent().getX(),e.getComponent().getY());
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static void main(String args[]){
new MyFrame();
}
}
This works fine for all the keys i tested except for Esc key. How can i enable it ?