简体   繁体   English

如何在ConfirmDialog中更改焦点?

[英]How do I change focus within ConfirmDialog?

I have a ConfirmDialog that pops up asking the user to enter their password. 我有一个ConfirmDialog弹出,要求用户输入密码。 I inserted a JPasswordField into the dialog to conceal the password. 我将JPasswordField插入对话框中以隐藏密码。 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 . 我目前遇到的问题是:当对话框弹出时,将焦点放在“确定”按钮而不是JPasswordField As shown below. 如下所示。

焦点问题的例子

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. 谢谢。

focus is given to the 'OK' button instead of the JPasswordField. 将焦点放在“确定”按钮而不是JPasswordField上。

Check out RequestFocusListener found in Dialog Focus RequestFocusListenerDialog Focus中的 RequestFocusListener

I would implement my own JDialog instatn of using JOptionsPane 我将使用JOptionsPane实现自己的JDialog JOptionsPane

OR you can try something like this: 或者,您可以尝试执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM