如何在Vaadin中对ComboBox进行排序?

时间:2014-08-29 10:14:29

标签: java combobox vaadin

我有一个ComboBox,并为数据提供BeanItemContainer。我还附加了一个用于默认排序的分拣机。但结果总是未分类。为什么呢?

public class Car {
    private String name;
}

private ComboBox box = new ComboBox("sorted cars");
BeanItemContainer<Car> cont = new BeanItemContainer<>(Car.class);
//cont.addItem();...
System.out.prinltn(cont.getSortableContainerPropertyIds()); //prints: [name]
box.setItemSorter(new DefaultItemSorter());
box.sort(new Object[] {"name"}, new boolean[] {true});
box.setContainerDataSource(cont);

1 个答案:

答案 0 :(得分:1)

似乎你必须为你的类中的可排序属性生成getter和setter

public static class Car {
    private String name;

    public Car(String name){
        this.name = name;
    }


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

此外,您需要在将容器分配到组合框之前对其进行排序

BeanItemContainer<Car> cont = new BeanItemContainer<>(Car.class);
cont.addItem(new Car("Z"));
cont.addItem(new Car("B"));
cont.addItem(new Car("Y"));
cont.addItem(new Car("A"));

cont.sort(new Object[] {"name"}, new boolean[] {true});
comboBox_sort.setContainerDataSource(cont);

我测试了这段代码。它应该工作。