You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CreatingATextFieldForIntegerOnlyInputUsingADataSource.asciidoc 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ---
  2. title: Creating A Text Field For Integer Only Input Using A Data Source
  3. order: 30
  4. layout: page
  5. ---
  6. [[creating-a-textfield-for-integer-only-input-using-a-data-source]]
  7. = Creating a TextField for integer only input using a data source
  8. A `TextField` is a component that always has a value of type `String`. When
  9. binding a property of another type to a text field, the value is
  10. automatically converted if the conversion between the two types is
  11. supported.
  12. [source,java]
  13. ....
  14. public class MyBean {
  15. private int value;
  16. public int getValue() {
  17. return value;
  18. }
  19. public void setValue(int integer) {
  20. value = integer;
  21. }
  22. }
  23. ....
  24. The property named "value" from a `BeanItem` constructed from `MyBean` will
  25. be of type `Integer`. Binding the property to a `TextField` will
  26. automatically make validation fail for texts that can not be converted
  27. to an `Integer`.
  28. [source,java]
  29. ....
  30. final MyBean myBean = new MyBean();
  31. BeanItem<MyBean> beanItem = new BeanItem<MyBean>(myBean);
  32. final Property<Integer> integerProperty = (Property<Integer>) beanItem
  33. .getItemProperty("value");
  34. final TextField textField = new TextField("Text field", integerProperty);
  35. Button submitButton = new Button("Submit value", new ClickListener() {
  36. public void buttonClick(ClickEvent event) {
  37. String uiValue = textField.getValue();
  38. Integer propertyValue = integerProperty.getValue();
  39. int dataModelValue = myBean.getValue();
  40. Notification.show("UI value (String): " + uiValue
  41. + "\nProperty value (Integer): " + propertyValue
  42. + "\nData model value (int): " + dataModelValue);
  43. }
  44. });
  45. addComponent(new Label("Text field type: " + textField.getType()));
  46. addComponent(new Label("Text field type: " + integerProperty.getType()));
  47. addComponent(textField);
  48. addComponent(submitButton);
  49. ....
  50. With this example, entering a number and pressing the button causes the
  51. value of the `TextField` to be a `String`, the property value will be an
  52. `Integer` representing the same value and the value in the bean will be
  53. the same int. If e.g. a letter is entered to the field and the button is
  54. pressed, the validation will fail. This causes a notice to be displayed
  55. for the field. The field value is still updated, but the property value
  56. and the bean value are kept at their previous values.