Java Swing:为JDialog禁用typeahead?
我有一个使用JDialog弹出窗口的应用程序。我使用的是1.6(客户的兼容性要求)。回车键将关闭对话框,并获取以前的结果。问题在于,用户可以在按下ENTER键(或任何其他文本)时提前键入 下面是一个片段:Java Swing:为JDialog禁用typeahead?,java,swing,jdialog,Java,Swing,Jdialog,我有一个使用JDialog弹出窗口的应用程序。我使用的是1.6(客户的兼容性要求)。回车键将关闭对话框,并获取以前的结果。问题在于,用户可以在按下ENTER键(或任何其他文本)时提前键入 下面是一个片段: private void promptForCredentials(final GenericSwitch sw) { final JDialog jd = new JDialog(); jd.setModal(true); jd.getContentPane().se
private void promptForCredentials(final GenericSwitch sw) {
final JDialog jd = new JDialog();
jd.setModal(true);
jd.getContentPane().setLayout(new FlowLayout());
final JLabel l_user = new JLabel("Username: ");
final JLabel l_pass = new JLabel("Password: ");
final JTextField tf_user = new JTextField(25);
final JTextField tf_pass = new JPasswordField(25);
JButton b_ok = new JButton("OK");
JButton b_same = new JButton("Same as previous");
JButton b_cancel = new JButton("Cancel");
final JLabel l_authfail = new JLabel("Authentication failed, try again.");
ActionListener okSameListener = new ActionListener() {
public void actionPerformed(ActionEvent ev) {
// if (((JButton) ev.getSource()).isVisible()) {
// return; // always returns for 'OK' button!
// }
String name = ((JButton) ev.getSource()).getText();
if (name.equals("OK")) {
// If user hits OK, save user/pw, unless they're both empty
// (treat that as "use last")
if (! tf_user.getText().isEmpty()
&& ! tf_pass.getText().isEmpty()) {
mLastUsername = tf_user.getText();
mLastPassword = tf_pass.getText();
}
}
jd.dispose();
sw.setUsername(mLastUsername, true);
sw.setPassword(mLastPassword, true);
if (!sw.checkLogin()) {
// auth failed so don't save for next time
mLastUsername = "";
mLastPassword = "";
}
display(sw.mIndex);
poll();
}
};
b_ok.addActionListener(okSameListener);
b_same.addActionListener(okSameListener);
b_cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
System.out.print("Poll cancelled\n");
abort();
jd.dispose();
}
});
Box box0 = Box.createHorizontalBox();
Box box1 = Box.createHorizontalBox();
Box box2 = Box.createHorizontalBox();
Box box3 = Box.createHorizontalBox();
box0.add(l_authfail);
box1.add(l_user);
box1.add(tf_user);
box2.add(l_pass);
box2.add(tf_pass);
box3.add(b_ok);
box3.add(Box.createHorizontalStrut(10));
if (!mLastUsername.isEmpty() && !mLastPassword.isEmpty()) {
box3.add(b_same);
box3.add(Box.createHorizontalStrut(10));
}
box3.add(b_cancel);
if (sw.authFailed()) {
jd.add(box0);
}
jd.add(box1);
jd.add(box2);
jd.add(box3);
SwingUtilities.getRootPane(b_ok).setDefaultButton(b_ok);
jd.setTitle("Login for " + sw.mShortname);
jd.setPreferredSize(new Dimension(400, 150));
jd.setSize(new Dimension(400, 150));
jd.setLocationRelativeTo(getContentPane());
jd.setVisible(true);
}
由于该应用程序轮询了大量外部资源,其中大多数资源通常具有相同的用户名/pw,因此第一次点击“回车”循环遍历该批资源是很好的。一切都好。(是的,我知道我应该有“以后使用相同的”复选框。也许以后。)
然而,我惊奇地注意到我可以提前打字。坦率地说,我喜欢这个,但我不认为它真的适合一般用户。如何禁用它
在将JDialog设置为可见之前使用所有操作是否有效?我试着检查按钮是否可见,但是当取消按钮可见时,确定按钮却不可见!(以上已注释)
谢谢也许是我,但我不能100%确定你的确切问题是什么。如果你没有得到一个体面的解决方案,请考虑更详细地描述,包括你观察到的VS预期行为。谢谢看,@ HovercraftFullOfEels。我想要的是,在ENTER(或单击OK)之后键入的任何文本都将被丢弃,直到出现下一个弹出窗口——不允许提前键入。然而,我想得越多,对于我的特定应用程序,意外的错误输入不会造成任何伤害(它只会再次询问creds是否错误)。所以,这个问题现在是假设性的,而不是实际的。顺便说一句,我从搜索中看到,虽然在SE 1.6中保留了前置类型,但在1.7中丢失了前置类型。