How to set object list for Vaadin 8 combobox

I want to combine a list of complex objects in a Vaadin combo box. I tried this as follows and it only shows garbage values ​​(toString () values). But I want to know how to set a specific attribute that should be displayed in the drop-down list.

enter image description here

Listed below are the class objects in the drop-down list.

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 cannot override the toString () method, as I will use it for other purposes.

+4
source share
1 answer

First, you can specify the type of combo box as follows when you create it.

private ComboBox<TestExecution> comboExecution = new ComboBox<>("Select Execution");

, , ItemCaptionGenerator.

comboExecution.setItemCaptionGenerator(new ItemCaptionGenerator<TestExecution>() {
        @Override
        public String apply(TestExecution execution) {
            return execution.getName();
        }
    });

, lamda .

comboExecution.setItemCaptionGenerator(execution -> execution.getName());
+6

Source: https://habr.com/ru/post/1680861/


All Articles