-
JOptionPane focus
I've tried several different ways to get userid/password using JOptionPane.
One was to use 2 different optionpanes to get id and pass separately. However, I would like to do it in one pass if possible. The following method works to get id/pass in one go, but the focus starts on the OK button. I would like it to focus on the userField so the user can just start typing.
Better yet would be a standard dialog for doing this, but... 
Code:
/*
* A return value of false means the user hit cancel.
*/
private boolean login() {
JTextField userField = new JTextField();
JPasswordField passField = new JPasswordField();
String message = "Please enter your user name and password.";
Object[] options = { message, userField, passField };
//Pop up the login dialog
int result = JOptionPane.showOptionDialog(this,
options,
"Login", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, null, null);
//user hit OK
if (result == JOptionPane.OK_OPTION) {
String id = userField.getText();
String pass = new String( passField.getPassword() );
//Try to validate the user using the provided id/pass
user = getLogin(id, pass);
return true;
}
//user hit cancel
return false;
}
Your help would be much appreciated.
-
Code:
userField.requestFocus()
...?
-
Nope. Already thought of that, but it had no effect.
-
Maybe the problem is that you pass your options along with message as the "message" parameter to the showOptionDialog(...) method. Have a try with
Code:
int result = JOptionPane.showOptionDialog(this,
message,
"Login", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,new Object[] {userField, passField},null);
cf. API doc
Or maybe (additionally) passing userField as "initialValue" (last parameter) helps...
If that doesn't work either I'm afraid you'll have to instantiate (and appropriately initialize) your own JDialog.
----
Those swing-guys are nuts. I always knew it.
Last edited by meisl; 04-11-2005 at 05:38 PM.
-
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|