JTextfieldをテキスト検証でバインドし、pojoモデルにバインドしようとしています。私の目標は、特定のテキスト長の許容文字セットをユーザーが入力できるようにし、バインディングを使用してモデルのテキストを設定することです。コードスニペットを以下に示します。データ検証とBeansbindingを使用したJtextField
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; } }
私はModalVOクラスで必要なプロパティの変更を発射するAspectJのを使用しています。 (これを達成するためにこのリンクに続いて:: http://yakafokon.wordpress.com/2008/12/02/beans-binding-jsr-295-with-pojo-and-aop/#comments)。
私の問題は、バインディングを使用しないと、検証は正しく行われますが、テキストはモーダルではありません。しかし、テキストフィールドをバインドすると、テキストはモデルに正しく設定されますが、検証は機能しません。検証とバインディングの両方を併用しているときに、なぜそれが機能しないのか、誰にも分かりませんか?
よろしく、 GeorgeB
ありがとう、それは働いた... –