JOptionPane focus


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: JOptionPane focus

Hybrid View

  1. #1
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99

    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.

  2. #2
    Join Date
    Mar 2005
    Location
    Sendling, MUC, .de
    Posts
    100
    Code:
    userField.requestFocus()
    ...?

  3. #3
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    Nope. Already thought of that, but it had no effect.

  4. #4
    Join Date
    Mar 2005
    Location
    Sendling, MUC, .de
    Posts
    100
    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 06:38 PM.

  5. #5
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    thx anyhow.

  6. #6
    Join Date
    Mar 2007
    Location
    Wausau, WI
    Posts
    1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center