This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
i'm trying to close a window with a close-button when hitting the return key of my keyboard. but when hitting the key the only thing that happens is that the button gets selected (i've set it as default button). i've put a keylistener to my close button. does anyone know how to do that?
KeyListeners on buttons? Hardly standard practice. You ought to use what they call "high-level" listeners on buttons, eg ActionListeners.
If you are still planning to use a KeyListener, you have to make sure it can receive the focus, add a KeyListener to it, then get a KeyEvent and get the (int) type of key from that KeyEvent, then if it is the type you want, set off the action.
Before you do that, try your app again, and instead of an enter key, try the space bar.
I trust you have read about KeyListeners in the Java Tutorial?[/edit] [ August 16, 2006: Message edited by: Campbell Ritchie ]
Joined: Jun 18, 2004
thanks for your answer. well it actually works with any other key. if set the close button as default button, meaning when i hit the return key the button gets selected but nothing happens. even when i hit enter again. but when i press any other key while the button is selected the window closes. i don't really understand why it it like that. do i have to treat the return-key special?