根据 JTextField 验证暂时禁用 JDialog 上的“确定”按钮
我创建了一个 JDialog 以类似的方式这,根据 Oracle 的教程。
使用 JOptionPane 构造函数:
optionPane = new JOptionPane(array,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION,
null,
options,
options[0]);
我没有引用“是”和“否”按钮,因为它们是由 JOptionPane 构造函数创建的。
现在,在我的对话框中,我有一个 JFormattedText 字段,其中包含我创建的 InputValidator,它持续验证文本字段的输入:
public class ValidatedDoubleField extends InputVerifier implements DocumentListener {
private JTextField field;
private Border defaultBorder;
public ValidatedDoubleField(JFormattedTextField f){
this.field = f;
this.defaultBorder = f.getBorder();
f.getDocument().addDocumentListener(this);
}
@Override
public boolean verify(JComponent input) {
//System.out.println("verify");
if (input instanceof JTextField){
JTextField f = (JTextField)input;
try{
Double value = Double.parseDouble(f.getText().replace(',', '.'));
return true;
}catch (NumberFormatException e){
return false;
}
}else if (input instanceof JFormattedTextField){
JFormattedTextField f = (JFormattedTextField)input;
try{
Double value = Double.parseDouble(f.getText().replace(',', '.'));
return true;
}catch (NumberFormatException e){
return false;
}
}
return false;
}
public boolean shouldYieldFocus(JComponent input){
boolean inputOK = verify(input);
if (inputOK) {
if (input instanceof JTextField){
JTextField f = (JTextField)input;
f.setBorder(defaultBorder);
return true;
}else if (input instanceof JFormattedTextField){
JFormattedTextField f = (JFormattedTextField)input;
f.setBorder(defaultBorder);
return true;
}else
return false;
} else {
if (input instanceof JTextField){
JTextField f = (JTextField)input;
f.setBorder(BorderFactory.createLineBorder(Color.red));
Toolkit.getDefaultToolkit().beep();
return false;
}else if (input instanceof JFormattedTextField){
JFormattedTextField f = (JFormattedTextField)input;
f.setBorder(BorderFactory.createLineBorder(Color.red));
Toolkit.getDefaultToolkit().beep();
return false;
}else
return false;
}
//return true;
}
@Override
public void changedUpdate(DocumentEvent e) {
this.field.getInputVerifier().shouldYieldFocus(field);
}
@Override
public void insertUpdate(DocumentEvent e) {
this.field.getInputVerifier().shouldYieldFocus(field);
}
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
this.field.getInputVerifier().shouldYieldFocus(field);
}
}
我发布了 InputVerifier 代码,即使它与问题不太相关。
现在我想做的是暂时禁用“确定”按钮,直到验证该字段,但我没有引用它。
我怎样才能做到这一点?
我正在寻找类似的东西:
JButton b = optionPane.getOkButton();
if (myFieldNotValidate)
b.setEnabled(false);
I have a JDialog created in a fashion like this, accordingly to Oracle's tutorial.
using the JOptionPane constructor:
optionPane = new JOptionPane(array,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION,
null,
options,
options[0]);
I have no reference to the "yes" and "no" buttons, because they are created by the JOptionPane constructor.
Now, in my dialog I have a JFormattedText field with a InputValidator created by me that continuosly validate text field's input:
public class ValidatedDoubleField extends InputVerifier implements DocumentListener {
private JTextField field;
private Border defaultBorder;
public ValidatedDoubleField(JFormattedTextField f){
this.field = f;
this.defaultBorder = f.getBorder();
f.getDocument().addDocumentListener(this);
}
@Override
public boolean verify(JComponent input) {
//System.out.println("verify");
if (input instanceof JTextField){
JTextField f = (JTextField)input;
try{
Double value = Double.parseDouble(f.getText().replace(',', '.'));
return true;
}catch (NumberFormatException e){
return false;
}
}else if (input instanceof JFormattedTextField){
JFormattedTextField f = (JFormattedTextField)input;
try{
Double value = Double.parseDouble(f.getText().replace(',', '.'));
return true;
}catch (NumberFormatException e){
return false;
}
}
return false;
}
public boolean shouldYieldFocus(JComponent input){
boolean inputOK = verify(input);
if (inputOK) {
if (input instanceof JTextField){
JTextField f = (JTextField)input;
f.setBorder(defaultBorder);
return true;
}else if (input instanceof JFormattedTextField){
JFormattedTextField f = (JFormattedTextField)input;
f.setBorder(defaultBorder);
return true;
}else
return false;
} else {
if (input instanceof JTextField){
JTextField f = (JTextField)input;
f.setBorder(BorderFactory.createLineBorder(Color.red));
Toolkit.getDefaultToolkit().beep();
return false;
}else if (input instanceof JFormattedTextField){
JFormattedTextField f = (JFormattedTextField)input;
f.setBorder(BorderFactory.createLineBorder(Color.red));
Toolkit.getDefaultToolkit().beep();
return false;
}else
return false;
}
//return true;
}
@Override
public void changedUpdate(DocumentEvent e) {
this.field.getInputVerifier().shouldYieldFocus(field);
}
@Override
public void insertUpdate(DocumentEvent e) {
this.field.getInputVerifier().shouldYieldFocus(field);
}
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
this.field.getInputVerifier().shouldYieldFocus(field);
}
}
I posted the InputVerifier code even if it's not so relevant for the question.
Now what I would like to do is temporarily disable the "ok" button until the field will be validated, but I haven't a reference to it.
How can I do that?
I'm looking for something like:
JButton b = optionPane.getOkButton();
if (myFieldNotValidate)
b.setEnabled(false);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试类似的方法来找到 JOptionPane 对话框中的按钮。
You can try something like this to locate buttons at JOptionPane Dialog.