简体   繁体   English

验证JGoodies绑定中的缓冲值

[英]Validating a buffered value in JGoodies Binding

I use the code: 我使用代码:

JTextField textField = BasicComponentFactory.createFormattedTextField(valueModel, numberFormatter);

This causes the JTextfield to work very nicely, it automatically changes 512.1!5 to 512.1 with no errors when I commit the trigger that is bound to the presentation model. 这将导致JTextfield很好地工作,当我提交绑定到表示模型的触发器时,它会自动将512.1!5更改为512.1,而不会发生错误。 This is great how smoothly it works but I would like to popup a JDialog saying hey, you inputted 512.1!5 which contained an invalid value so that the user knows exactly what happened. 这样的运行非常顺畅,但是我想弹出一个JDialog,说,嘿,您输入了512.1!5,其中包含无效值,以便用户确切知道发生了什么。 I cannot find a hook in JGoodies Binding to set that up. 我在JGoodies Binding中找不到钩子来进行设置。 I tried adding a propertyChangeListener on my PresentatonModel's getBufferedComponentModel() but it would only report values that already had the ! 我尝试在我的PresentatonModel的getBufferedComponentModel()上添加一个propertyChangeListener,但是它只会报告已经具有!的值。 character missing. 字符丢失。 Therefore I am lost on how to detect a format error, is there some JGoodies bean listener that will allow me to know when to alert the user? 因此,我不知道如何检测格式错误,是否有一些JGoodies bean侦听器可以让我知道何时向用户发出警报?

You can use jgoodies validator interface to validate whatever input there is on that textfield Write your own validator class which implements the following interface. 您可以使用jgoodies验证器接口来验证该文本字段上的任何输入。编写您自己的验证器类,该类实现以下接口。 For instance the class name be TextFieldValidator. 例如,类名称为TextFieldValidator。

Validator<T>

Precondition is that you bind your textfield to a PresentationModel. 前提条件是将文本字段绑定到PresentationModel。

TextFieldValidator<T> validator = (TextFieldValidator<T>) getPresentationModel().getValidator();
        validator.setComponent(getYourTextField());

setComponent will set the JTextField reference in TextFieldValidator class and in the validate() method you can show pop ups/change background colors or what not. setComponent将在TextFieldValidator类中设置JTextField引用,在validate()方法中,您可以显示弹出窗口/更改背景颜色或不显示背景颜色。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM