GWT的自定义正整数TextField

这里关键是两个类PropertyEditorValidator

package com.yingxia.tonglin.client.commoncontrol;

import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.PropertyEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;

public class PositiveNumberField extends TextField<Integer> {

    public PositiveNumberField() {
       
        PropertyEditor<Integer> propertyEditor = new PropertyEditor<Integer>() {
           
            @Override
            public String getStringValue(Integer value) {
                return value.toString();
            }
           
            @Override
            public Integer convertStringValue(String value) {
                return Integer.valueOf(value);
            }
        };
        this.setPropertyEditor(propertyEditor);
       
        this.setAllowBlank(false);
       
        Validator validator = new Validator() {
           
            @Override
            public String validate(Field<?> field, String value) {
                String regex = "^[1-9][0-9]*$";
                if(!value.matches(regex)) {
                    return "请填写合法数字";
                }
                return null;
            }
        };
        this.setValidator(validator);
    }
   
}
 

你可能感兴趣的:(UI,gwt)