具有数据验证和 Beans 绑定的 JtextField

发布于 2024-10-13 03:57:31 字数 1889 浏览 5 评论 0原文

我正在尝试将 JTextfield 与文本验证绑定,然后将其绑定到 pojo 模型。我的目标是允许用户输入特定文本长度的某些允许字符集,并使用绑定在模型中设置文本。代码片段如下。

public class TestValidationBinding {
    private JTextField field;
    private ModelVo modelVo;

    public TestValidationBinding() {
        field = new JTextField();
        modelVo = new ModelVo();
        field.setDocument(new PlainDocument() {
            private static final long serialVersionUID = 1L;

            @Override
            public void insertString(int offs, String str, AttributeSet a)
                    throws BadLocationException {
                // other validation for key typing, check length
                int limit = 15;
                if (str == null) {
                    return;
                }

                if ((getLength() + str.length()) <= limit) {
                    super.insertString(offs, str, a);
                }
            }
        });

        Property srcProperty = BeanProperty.create("text");
        Property tgtProperty = BeanProperty.create("text");
        AutoBinding binding = Bindings
                .createAutoBinding(UpdateStrategy.READ_WRITE, field,
                        srcProperty, modelVo, tgtProperty);
        binding.bind();

    }
}

ModelVO 类是:

public class ModelVo {
    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        System.out.println("Text is:" + text);
        this.text = text;
    }
}

我正在使用 AspectJ 来触发 ModalVO 类中必要的属性更改。 (按照此链接实现此目的:: http://yakafokon.wordpress.com/2008/12/02/beans-binding-jsr-295-with-pojo-and-aop/#comments)。

现在,我的问题是,如果我不使用绑定,验证会正确完成,但文本不会在模式中设置。但是,如果我绑定文本字段,文本会在模型中正确设置,但验证不起作用。任何人都可以提供见解,为什么当我同时使用验证和绑定时它不起作用?

I am trying to bind a JTextfield with text validation and then bind it to a pojo model. My target is to allow user to type certain set of allowable characters with specific text length, and set the text in the model using binding. The code snippet is given below.

public class TestValidationBinding {
    private JTextField field;
    private ModelVo modelVo;

    public TestValidationBinding() {
        field = new JTextField();
        modelVo = new ModelVo();
        field.setDocument(new PlainDocument() {
            private static final long serialVersionUID = 1L;

            @Override
            public void insertString(int offs, String str, AttributeSet a)
                    throws BadLocationException {
                // other validation for key typing, check length
                int limit = 15;
                if (str == null) {
                    return;
                }

                if ((getLength() + str.length()) <= limit) {
                    super.insertString(offs, str, a);
                }
            }
        });

        Property srcProperty = BeanProperty.create("text");
        Property tgtProperty = BeanProperty.create("text");
        AutoBinding binding = Bindings
                .createAutoBinding(UpdateStrategy.READ_WRITE, field,
                        srcProperty, modelVo, tgtProperty);
        binding.bind();

    }
}

The ModelVO class is:

public class ModelVo {
    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        System.out.println("Text is:" + text);
        this.text = text;
    }
}

I am using AspectJ to fire necessary property changes in the ModalVO class.
(followed this link to achieve this:: http://yakafokon.wordpress.com/2008/12/02/beans-binding-jsr-295-with-pojo-and-aop/#comments ).

Now, my problem is if I do not use binding, the validation is done properly but the text is not set in modal. But if I bind textfield, the text is set in the model properly, but validation does not work. Could anyone provide insight why it is not working when I am using both validation and binding together?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一梦等七年七年为一梦 2024-10-20 03:57:31

尝试使用javax.swing.text.DocumentFilter而不是extendint PlainDocument

Try to use javax.swing.text.DocumentFilter instead of extendint the PlainDocument.

原野 2024-10-20 03:57:31

如果您使用绑定,请尝试覆盖 replace 方法而不是 insertString

Try to override replace method instead of insertString if You use binding.

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