Reputation: 187
I have a small problem with Vaadin and Spring Boot. When I want to change convert int to string I get this error. I know it's probably a trivial problem but I'm just starting to learn Spring Boot.
com.vaadin.flow.component.textfield.IntegerField@1c7a3814
@Route("main")
public class vaadinapp extends VerticalLayout {
public vaadinapp() {
IntegerField integerField = new IntegerField("Age");
Button buttonName = new Button("Left", new Icon(VaadinIcon.ACADEMY_CAP));
Label labelName = new Label();
String integerFieldStr = integerField.toString();
buttonName.addClickListener(clickEvent -> {
labelName.setText(integerFieldStr);
});
add(integerField, buttonName, labelName);
}
}
Upvotes: 0
Views: 188
Reputation: 81
Invoking the toString
method of the integerField
object will return you string representation of the object, not the value, stored in it. To retrieve the value you could use the getValue
method instead. Like this:
public vaadinapp() {
IntegerField integerField = new IntegerField("Age");
Button buttonName = new Button("Left", new Icon(VaadinIcon.ACADEMY_CAP));
Label labelName = new Label();
buttonName.addClickListener(clickEvent -> {
String integerFieldStr =
Optional.ofNullable(integerField.getValue()) //you need this check since getValue could return null
.map(String::valueOf)
.orElse("");
labelName.setText(integerFieldStr);
});
add(integerField, buttonName, labelName);
}
UPD: I'd also moved the value retrieval code into the listener. Thanks Eric for this point.
Upvotes: 2