使用Controls FX进行TextField/组件验证 [英] TextField/Component Validation with Controls FX

查看:93
本文介绍了使用Controls FX进行TextField/组件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个像这样的文本字段:

I'm trying to implement a textfield like this one :

文本字段验证http://imageshack.com/a/img537/8329/FSht8P .png

我的目标是确定TextField的文本是Double还是其他(然后以红色显示). 我想使用 ControlsFX 来学习该库并且不编辑CSS,但是它不能很好地工作,我迷失在Javadoc中.有没有人有一个示例,或者可以帮助我改善我的代码?

My goal is to identify if the text of the TextField is a Double or something else (then it appears in red). I want to use ControlsFX to learn the library and to not edit the css, but it doesn't work very well, and I'm lost in the javadoc. Does anyone have an example or can help me improve my code ?

这是我尝试做的事情:

Validator<Double> checkTextField = new Validator<Double>() {

        @Override
        public ValidationResult apply(Control t, Double u) {
            ValidationResult vd = new ValidationResult();
            if(Double.valueOf(t.toString()) != null){
                vd.addErrorIf(t, "false", false);
            }
            return vd;

        }

    };


    ValidationSupport validationSupport = new ValidationSupport();
    validationSupport.registerValidator(latitudeTextField,  checkTextField/*Validator.createEmptyValidator("Text is required")*/);
    ValidationDecoration iconDecorator = new GraphicValidationDecoration();
    ValidationDecoration cssDecorator = new StyleClassValidationDecoration();
    ValidationDecoration compoundDecorator = new CompoundValidationDecoration(cssDecorator, iconDecorator);
    validationSupport.setValidationDecorator(compoundDecorator);

推荐答案

您确实犯了几个错误.首先,您可以看一下 ControlsFX展示柜.他们的展示柜中总是有几个代码示例.

You indeed make several mistakes. First you can have a look at the ControlsFX Showcase. There are always several code examples within their showcase.

接下来,您的示例期望TextField为验证器提供Double值,但是您很可能知道TextField本身仅对String起作用,因此您将需要String Validator.

Next your example expects the TextField to serve a Double to the Validator, but as you problably know the TextField itself only operates with Strings, so you will need a String Validator.

然后尝试打印control.toString()将要打印的任何内容,这很可能不是它表示的文本,而是有关控件本身的一些信息.您需要的是文本,该文本当前显示在文本字段中.为此,您不必询问控件,因为在您的情况下,文本值已经以值参数"u"的形式提供给您.

Then you try whatever control.toString() will print, which is most likely not the text it represents but some information about the control itself. What you want is the Text, which is currently shown in the textfield. For that you will not have to ask the control, since the text-value is already given to you in form of the value parameter 'u' in your case.

因此,您现在要检查的是,是否可以将String解析为双精度型.从文档您可以找到一个正则表达式来检查是否存在这种情况(不知道您的情况是否有更好的正则表达式).

So what you want to do now is, you want to check, if the String could be parsed into a double. From the Documention you can find a Regex to check if this is the case (don't know if there are any better regexes for your case).

所以总的来说,看起来像这样:

So all together it could look like this:

    ValidationSupport support = new ValidationSupport();

    Validator<String> validator = new Validator<String>()
    {
      @Override
      public ValidationResult apply( Control control, String value )
      {
        boolean condition =
            value != null
                ? !value
                    .matches(
                        "[\\x00-\\x20]*[+-]?(((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)([eE][+-]?(\\p{Digit}+))?)|(\\.((\\p{Digit}+))([eE][+-]?(\\p{Digit}+))?)|(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*" )
                : value == null;

        System.out.println( condition );

        return ValidationResult.fromMessageIf( control, "not a number", Severity.ERROR, condition );
      }
    };

    support.registerValidator( textfield, true, validator );

这篇关于使用Controls FX进行TextField/组件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆