• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

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

 
Ranch Hand
Posts: 64
  • 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
 
Marshal
Posts: 64643
225
  • 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.
 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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
Boost this thread!