This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help Me Click the Default Button

 
T Eakins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
T Eakins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this is why Nathan is the king!
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice example Nathan!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic