aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help Me Click the Default Button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help Me Click the Default Button" Watch "Help Me Click the Default Button" New topic
Author

Help Me Click the Default Button

T Eakins
Greenhorn

Joined: Feb 20, 2004
Posts: 9
Hi,
I've got a popup control to capture a password. However, I can't get the 'OK' button to be the default button such that when I type in a password and hit enter, the OK button would be clicked. Instead, I have to tab out and hit space, or I have to click it with the mouse button.
I've tried many combinations. The most recent attempt (listed, below) is to capture the VK_ENTER, move focus to the next control (the 'OK' button), and press the space key.
I must be doing something wrong, though. I get the focus to move forward, but no luck with clicking the space.
Any help is greatly appreciated!!

[ February 20, 2004: Message edited by: Tim E. ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You can add an ActionListener to a JPasswordField. So something like....

You don't have to pass the ActionEvent to your loginAction method if you don't want to. Only if you need access to that object.
Another thing you can try which I can't get to work 100% of the time is:
getRootPane().setDefaultButton(submitButton);
Sometimes this works for me, other times it doesn't. That's why I usually just do it the first way I suggested. It works all the time.
[ February 20, 2004: Message edited by: Gregg Bolinger ]

GenRocket - Experts at Building Test Data
T Eakins
Greenhorn

Joined: Feb 20, 2004
Posts: 9
Thanks for the help.
Unfortunately, I don't have the submitButton handle because I am using the java's standard modal dialog boxes:

Maybe it just can't be done without creating my own dialog box. Any other thoughts?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, ShowInputDialog() does what you want. Problem with that is I don't know of a way to specify a JPasswordField so your input wouldn't be masked. The only way to do what you want is to write a custom dialog by extending JDialog.
T Eakins
Greenhorn

Joined: Feb 20, 2004
Posts: 9
Yep, I've tried a lot of different approaches and I didn't see a way, either. That seemed like something that should be available because I could envision a scenario where I needed to add some other type of ActionListener to the OK button before processing.
Thanks again for your help. I'll write a small custom dialog box to force it.
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
How strange that this isn't supported...
What JDK version?
Have you looked at the source for JOptionPane to see if it attempts such functionality?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Here's how I got it to work... the only issue I would like to fix would be how to get the username field to be the initial selection when the optionpane comes up. I tried adding a component listener to the panel and to the username text field, but neither worked...



(NEP - removed long line in code that was widening the page )
[ February 24, 2004: Message edited by: Nathan Pruett ]

-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

And this is why Nathan is the king!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Nice example Nathan!


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help Me Click the Default Button