File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes enter key events in a page containing 2 or more than 2 buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "enter key events in a page containing 2 or more than 2 buttons" Watch "enter key events in a page containing 2 or more than 2 buttons" New topic
Author

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

praveen shinde
Ranch Hand

Joined: Jun 03, 2009
Posts: 63
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

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Jul 26, 2007
Posts: 9990
    
    7

Check out key bindings


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

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

Joined: Jul 26, 2007
Posts: 9990
    
    7

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

Joined: May 03, 2008
Posts: 4523
    
    5

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/


luck, db
There are no new questions, but there may be new answers.
 
Don't get me started about those stupid light bulbs.
 
subject: enter key events in a page containing 2 or more than 2 buttons
 
Similar Threads
Regarding Action and KeyEvents
2 buttons listening for the same key
button action command from keylistener
Which JButton is being pressed ? OK or Cancel ?
Pressing ENTER when Button is selected doesnt fire buttonActionPerformed...