如何在Vaadin中对ComboBox进行排序? [英] How to sort a ComboBox in Vaadin?

查看:43
本文介绍了如何在Vaadin中对ComboBox进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a ComboBox and provide the data with a BeanItemContainer. I also attach a sorter for default sorting. But the result is always unsorted. Why?

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


推荐答案

似乎您必须为可排序对象生成getter和setter

seems you have to generate getters and setters for the sortable property in your class

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

另外,在分配容器之前,还需要对其进行排序

Also you need to sort your container before you assign it to your combobox

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

我测试了这段代码。应该可以。

I tested this code. It should work.

这篇关于如何在Vaadin中对ComboBox进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆