Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set object list to Vaadin 8 combobox

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.

enter image description here

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.

like image 991
Dilu Avatar asked Jul 06 '17 07:07

Dilu


1 Answers

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());
like image 100
Hiran Perera Avatar answered Oct 27 '22 04:10

Hiran Perera