1

我有以下代码:

JTextField uname = new JTextField(defaultUser);
JPasswordField passwd = new JPasswordField();
JTextField serverAddress = new JTextField(defaultServer);
JTextField port = new JTextField(Integer.toString(defaultPort));
final JComponent[] inputs = new JComponent[]{new JLabel("Username"), uname, new JLabel("Password"), passwd, new JLabel("Server Address"), serverAddress, new JLabel("Server Port"), port};


int var = JOptionPane.showConfirmDialog(parent, inputs, "Enter Connection Details", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (var != 0) {
    return;
}
....

这将创建一个对话框,提示输入一些详细信息以连接到服务器。我想要的是使密码字段成为默认选择。也就是说,光标默认在那里,因此您可以在窗格出现后立即开始输入,而不必先选择密码框 - 默认情况下,确定按钮具有焦点。

我已经尝试使用 passwd.requestFocusInWindow() 但它不起作用(因为我认为在调用它之前该框必须是可见的)。我还尝试用各种内容覆盖 passwd 的 requestFocus 方法,但它也没有飞起来(可能是因为它没有被调用......)

注意:我知道其他一些 JOptionPane 方法具有默认值参数,但是它们在布局输入框时存在问题,所以它们对我没有好处。

有人有什么想法吗?这不是交易破坏者,所以如果不能轻松完成,我不会太沮丧。

干杯

4

1 回答 1

0

尝试将 ComponentListener 添加到密码字段,当它变得可见时调用 #requestFocusInWindow

更新: JOptionPane使对话框中的 [OK] 按钮默认,并在显示对话框时将其聚焦。所以解决方案不是那么明显......我们必须添加 aHierarchyListener并等到密码字段添加到对话框中,然后检查对话框是否JRootPane有默认按钮,如果有,添加 a FocusListener,最后当按钮获得永久焦点时切换焦点到密码字段:

import static javax.swing.JOptionPane.OK_CANCEL_OPTION;
import static javax.swing.JOptionPane.PLAIN_MESSAGE;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FocusPasswordFieldInOptionPaneDemo implements Runnable
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new FocusPasswordFieldInOptionPaneDemo());
    }

    @Override
    public void run()
    {
        JTextField uname = new JTextField("user");
        JPasswordField passwd = new JPasswordField();
        JTextField serverAddress = new JTextField("server");
        JTextField port = new JTextField(Integer.toString(1337));
        final JComponent[] inputs = new JComponent[] {new JLabel("Username"), uname, new JLabel("Password"), passwd,
                new JLabel("Server Address"), serverAddress, new JLabel("Server Port"), port};

        makeSurePasswordFieldGetsFocus(passwd);

        int answer = JOptionPane.showConfirmDialog(null, inputs, "Enter Connection Details", OK_CANCEL_OPTION, PLAIN_MESSAGE);
        System.out.println(answer);
    }

    /**
     * {@link JOptionPane} makes the [OK] button default in the dialog and makes it focused.
     * <p>
     * So via a couple of listeners we can wait until the button gets permanent focus and then switch focus to the password field.
     */
    private void makeSurePasswordFieldGetsFocus(final JPasswordField passwd)
    {
        passwd.addHierarchyListener(new HierarchyListener()
        {
            HierarchyListener hierarchyListener = this;

            @Override
            public void hierarchyChanged(HierarchyEvent e)
            {
                JRootPane rootPane = SwingUtilities.getRootPane(passwd);
                if (rootPane != null)
                {
                    final JButton okButton = rootPane.getDefaultButton();
                    if (okButton != null)
                    {
                        okButton.addFocusListener(new FocusAdapter()
                        {
                            @Override
                            public void focusGained(FocusEvent e)
                            {
                                if (!e.isTemporary())
                                {
                                    passwd.requestFocusInWindow();
                                    passwd.removeHierarchyListener(hierarchyListener);
                                    okButton.removeFocusListener(this);
                                }
                            }
                        });
                    }
                }
            }
        });
    }
}

这个解决方案有效,但它有点老套......另一种方法是创建一个自定义的“DatabaseConnectionDialog”,类似于@syb0rg 的答案。

于 2012-12-31T00:44:29.263 回答