user2577094
user2577094

Reputation:

How do I change focus within ConfirmDialog?

I have a ConfirmDialog that pops up asking the user to enter their password. I inserted a JPasswordField into the dialog to conceal the password. That all works fine.

The issue I'm having at the moment is: when the dialog pops up, focus is given to the 'OK' button instead of the JPasswordField. As shown below.

Example of focus issue

I would like to know how I would go about changing the focus from the 'OK' button to the password field.

Here's the code I have so far:

JPasswordField passField = new JPasswordField();
        
int option = JOptionPane.showConfirmDialog(null, passField, "Enter password", JOptionPane.OK_CANCEL_OPTION);

I have no idea where to go from here so any help would be greatly appreciated.

Thanks.

Upvotes: 1

Views: 386

Answers (2)

camickr
camickr

Reputation: 324098

focus is given to the 'OK' button instead of the JPasswordField.

Check out RequestFocusListener found in Dialog Focus

Upvotes: 1

Khinsu
Khinsu

Reputation: 1487

I would implement my own JDialog instatn of using JOptionsPane

OR you can try something like this:

JPasswordField pass = new JPasswordField()         
{
    public void addNotify()             
    {                 
        super.addNotify();
        requestFocusInWindow();             
    }         
}; 

Upvotes: 0

Related Questions