【问题标题】:temporarily disable ok button on a JDialog, accordingly to a JTextField validation根据 JTextField 验证暂时禁用 JDialog 上的确定按钮
【发布时间】:2011-12-06 03:32:56
【问题描述】:

我根据 Oracle 的 tutorial 创建了一个 JDialog in a fashion like this

使用 JOptionPane 构造函数:

optionPane = new JOptionPane(array,
                JOptionPane.QUESTION_MESSAGE,
                JOptionPane.YES_NO_OPTION,
                null,
                options,
                options[0]);

我没有提到“是”和“否”按钮,因为它们是由 JOptionPane 构造函数创建的。

现在,在我的对话框中,我有一个带有我创建的 InputValidator 的 JFormattedText 字段,它可以持续验证文本字段的输入:

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);

【问题讨论】:

  • 指针here
  • @ring 持有者:谢谢。我用 camickr 解决方案解决了。太好了!

标签: java swing jtextfield jdialog inputverifier


【解决方案1】:

您可以尝试这样的方法来定位 JOptionPane 对话框中的按钮。

public class Snippet {

    public static void main(String[] args) {
        JOptionPane optionPane = new JOptionPane("Test",
                        JOptionPane.QUESTION_MESSAGE,
                        JOptionPane.YES_NO_OPTION,
                        null);

        List<JButton> buttons = new ArrayList<JButton>();

        loadButtons(optionPane, buttons);

    }

    public static void loadButtons(JComponent  comp, List<JButton> buttons) {
        if (comp == null) {
            return;     
        }

        for (Component c : comp.getComponents()) {
            if (c instanceof JButton) {
                buttons.add((JButton) c);

            } else if (c instanceof JComponent) {
                loadButtons((JComponent) c, buttons);
            }
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 2012-03-06
    • 2012-01-07
    • 1970-01-01
    相关资源
    最近更新 更多