Hi,
I have a dialog box with few buttons. How do I make a button "clicked" whenever it get focussed and I press "Enter" on the keyboard? Currently I have to use mouse and click on the button.
Thank you.
Printable View
Hi,
I have a dialog box with few buttons. How do I make a button "clicked" whenever it get focussed and I press "Enter" on the keyboard? Currently I have to use mouse and click on the button.
Thank you.
I assume that you want the actionPerformed() method to be called when you press Enter when the button has the focus.
Here's a class that does that. Add it as a KeyListener for the button:
button.addKeyListener(new MakeEnterDoAction());
public class MakeEnterDoAction extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
Object src = ke.getSource();
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(
new ActionEvent(src,
ActionEvent.ACTION_PERFORMED,
"Enter"));
}
} // end keyPressed()
} // end class
Norm
Thank you very much norm..
Ill put that in my program + with some notes for your contribution