Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I create a textfield which supports numbers only in vaadin

I am using a Vaadin text field and I want to restrict it to support numbers only in it. I tried to override setValue() and return without calling super. setValue() if text is not a number. But it doesn't seems to be working. How can I correct this? I am using Vaadin 7. And I think it doesn't support NumberField as well.

like image 587
Sanjaya Liyanage Avatar asked Jun 17 '13 09:06

Sanjaya Liyanage


2 Answers

In Vaadin 7, you can use a TextField and set a validator to allow only numbers:

TextField textField;
textField.addValidator(new RegexpValidator("[-]?[0-9]*\\.?,?[0-9]+"), "This is not a number!");

Change the regex to fit your needs. Remember that still is handling Strings and therefore you still need to convert the returning value of the TextField:

Long.parseLong(textField.getValue())
like image 148
King Midas Avatar answered Sep 17 '22 14:09

King Midas


With Vaadin 8, you can use Binder:

Binder<YouBean> binder = new Binder<>();
binder.forField(textField)
      .withConverter(new StringToIntegerConverter("Must be Integer"))
      .bind(YouBean::getter, YouBean::setter);
binder.setBean(bean);  //optional
like image 25
Ahmed Ashour Avatar answered Sep 18 '22 14:09

Ahmed Ashour