Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to close window with return key

 
Isaac Jones
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone

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?

regards
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

CR

[edit]I trust you have read about KeyListeners in the Java Tutorial?[/edit]
[ August 16, 2006: Message edited by: Campbell Ritchie ]
 
Isaac Jones
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey

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?

regards
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> (i've set it as default button)

how? post your code.

works OK in this
 
Isaac Jones
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm sorry, i'm talking about swt here, looks like you're talking about swing. maybe it works the same. this is what i have so far:



the do_login(e)-Method doesdn't do much yet. just closes the loginwindow as follows:



any ideas?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic