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.
04-11-2005, 04:16 PM
meisl
Code:
userField.requestFocus()
...?
04-11-2005, 04:50 PM
doredson
Nope. Already thought of that, but it had no effect.
04-11-2005, 05:05 PM
meisl
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... :rolleyes:
If that doesn't work either I'm afraid you'll have to instantiate (and appropriately initialize) your own JDialog.