Reputation: 83
I want to integrate list of complex objects to a Vaadin combobox. I tried it as follows and that shows only garbage values (toString() values). But I want to know how to set the specific attribute which should show in the drop down.
Below class objects should be rendered in the combobox.
public class TestExecution {
private String name;
private String startingTime;
private String endingTime;
private String status;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStartingTime() {
return startingTime;
}
public void setStartingTime(String startingTime) {
this.startingTime = startingTime;
}
public String getEndingTime() {
return endingTime;
}
public void setEndingTime(String endingTime) {
this.endingTime = endingTime;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Note: I can't override the toString() method as I will be using it for other purposes.
Upvotes: 4
Views: 2171
Reputation: 746
Firstly, you can give the type of the combo box as follows when creating it.
private ComboBox<TestExecution> comboExecution = new ComboBox<>("Select Execution");
Then you can specify the logic to render the caption of the items of the dropdown by setting a ItemCaptionGenerator.
comboExecution.setItemCaptionGenerator(new ItemCaptionGenerator<TestExecution>() {
@Override
public String apply(TestExecution execution) {
return execution.getName();
}
});
You can simplify the code using lamda expressions as follows.
comboExecution.setItemCaptionGenerator(execution -> execution.getName());
Upvotes: 8