根据 JTextField 验证暂时禁用 JDialog 上的“确定”按钮

发布于 2024-12-09 17:33:25 字数 3695 浏览 0 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2024-12-16 17:33:25

您可以尝试类似的方法来找到 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);
            }
        }
    }

}

You can try something like this to locate buttons at JOptionPane Dialog.

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文