如何在JOptionPane中设置默认输入字段

时间:2012-12-31 00:23:34

标签: java joptionpane

我有以下代码:

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方法都有默认值参数,但是它们在布局输入框时有问题,所以它们对我没有好处。

有人有任何想法吗?这不是一个交易破坏者,所以如果不能轻易做到,我不会太沮丧。

干杯

1 个答案:

答案 0 :(得分:0)

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

<强>更新 JOptionPane使对话框中的[OK]按钮成为默认值,并在显示对话框时将其聚焦。所以解决方案不是那么明显......我们必须添加HierarchyListener并等待密码字段添加到对话框,然后检查对话框的JRootPane是否有默认按钮,如果是,则添加一个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);
                                }
                            }
                        });
                    }
                }
            }
        });
    }
}

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