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