2014-08-29 54 views
0

我有一個ComboBox並提供數據與BeanItemContainer。我還附加一個分揀機進行默認分揀。但結果始終未排序。爲什麼?如何在Vaadin中對ComboBox進行排序?

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

看來你必須生成你的類

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); 

之前整理你的容器可排序的屬性getter和setter我測試了這個代碼。它應該工作。

相關問題