• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enter key events in a page containing 2 or more than 2 buttons

 
praveen shinde
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have a login frame which consist of 2 buttons: submit and change password buttons. When the focus is on the enter button and an enter key is pressed frame 1 should be opened. And when the focus is on the change password button and an enter key is pressed change password page should open.

The above process works when I chick on the button using the mouse. However I want the event to be fired when an enter key is pressed
Please help
Thanks
Praveen
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you are adding separate Listeners to each button?
I would have thought it would work like that, except you might have to use space instead of enter.

Try an SSCCE which prints out messages like "Button 1" and "Button 2" to the command line; then you can see what is happening without getting confused by several different frames appearing.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
I would have thought it would work like that, except you might have to use space instead of enter.

I think the enter instead of space used to work on Win 98 SE.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just thought of another way.
Disclaimer: I haven't tried it out.

You can set a default button for your frame using JRootPane#getDefaultButton()*
Suppose you set it to submit.
In the action for the submit, you can check which button has focus and then process it accordingly.

*Default button is fired when the user hits Enter
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen shinde wrote:When the focus is on the enter button and an enter key is pressed frame 1 should be opened. And when the focus is on the change password button and an enter key is pressed change password page should open.

For JDK 5 and above:

If you're stuck with an older version of Java, see Rob Camick's http://tips4java.wordpress.com/2008/10/25/enter-key-and-button/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic