具有数据验证和 Beans 绑定的 JtextField
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
javax.swing.text.DocumentFilter
而不是extendintPlainDocument
。Try to use
javax.swing.text.DocumentFilter
instead of extendint thePlainDocument
.如果您使用绑定,请尝试覆盖
replace
方法而不是insertString
。Try to override
replace
method instead ofinsertString
if You use binding.