How to sort a ComboBox?


I’m new to Vaadin, and still trying to wrap my head around how everything works. I’ve been going through the Tutorial and trying to test out new things along the way. One thing I have not been able to figure out is how to sort a ComboBox.


	private final ComboBox cities = new ComboBox("City", new IndexedContainer());
cities.addItem( "Something" );

I tried ((IndexContainer)cities.getContainerDataSource()).sort(new Object[]{}, new boolean[] {true}) but that didn’t work. I’m not sure what to use for the “PropertyId” array that needs to be sorted.

Can someone point me in the right direction please?




It’s been a week and I’m amazed that no one can answer how to perform this simple task? Is it really that complicated? I would have thought it fairly straight-forward, but when I look through the Vaadin code, I have to admit I get thoroughly lost with IndexedContainer$IndexContainerItem and their propertyIds, etc.

Can someone please help?



Try first sorting the elements in a List, check Collections.sort(List list) and then add this elements at the combobox.

ArrayList listOfTheElements=new ArrayList… put here the elements.

ArrayList listSorted=Collections.sort(listOfTheElements);
Iterator iterator=listSorted.iterator();



In your case, the items in the container actually do not have any normal properties you could sort them by - just item IDs. This is why the sort() method does not work. Almost every operation and component relies on container property values, but ComboBox also has a caption mode (enabled by default) that directly uses item identifiers as captions and ignores property values. Note that the parameter to Container.addItem(Object) and ComboBox.addItem(Object) is an item identifier, not the item content.

If you want to use the sorting API etc. you should first add the property on the container level (with correct type information) using addContainerProperty(), and then set values for the property you added right after the call to addItem(). Other containers such as BeanItemContainer might add the properties automatically, or you could use a small utility method to add the item and set the correct property value in one operation.

The Container API methods in ComboBox are mostly proxies for the corresponding methods of its container data source.

Sure. I realize that is a solution, but rather I was trying to understand the Vaadin framework and how to sort a combobox without having to pre-sort the data. Given that the container supported sorting, I’m trying to understand how to use it.



Thanks for the tip, but I am not sure I follow.

I can do the following, but I am not sure what to use/how to create a Property for the item. Can you please provide some sample code? I’ve tried:

private final ComboBox cities = new ComboBox("City", new IndexedContainer());
cities.addContainerProperty("city", String.class, null );

cities.addItem( "Something" ).addItemProperty( "Something", new ObjectProperty<String>("city")).;
((IndexContainer)cities.getContainerDataSource()).sort(new Object[]{"city"}, new boolean[]

but it throws an exception:

java.lang.UnsupportedOperationException: Indexed container item does not support adding new properties$IndexedContainerItem.addItemProperty(

Any suggestions or help would be appreciated. From what I understand, I would have to create an Item first, assign the property and then add the item to the container? But if I do that, and only add the item id, how does Vaadin know what the actual Item object it is referring to?



/* Creating a container, with a property of "name". Item Id is a number, here. Can be anything (unique).
 * Alternatively, you could use the IndexedContainer to generate it's own ItemId :
 * cityContainer.getItem(cityContainer.addItem()).getItemProperty("name").setValue("New York");
IndexedContainer cityContainer = new IndexedContainer();
cityContainer.addContainerProperty("name", String.class, null);
cityContainer.addItem(1).getItemProperty("name").setValue("New York");
cityContainer.getItem(cityContainer.addItem()).getItemProperty("name").setValue("New York");
/* Lets sort the container on ascending name*/
cityContainer.sort(new Object[]{"name"}, new boolean[]

/* Here's a comboBox that uses that container, where we are using the "name" property as the item caption */
ComboBox comboBox = new ComboBox("City", cityContainer);

Ahhh… Thanks! Now I see/understand how that works. I hadn’t realized that.

Thanks so much!


Sorry to Post my question here as I did not find a place where I can post my question.
My question is also related to combobox in Vaadin 6 and need to know one thing.

Actually, I have a combobox which is being populated by SQLContainer from DB. However I want None to be selected by default in Combobox List before and then SQL Values.

However when I try to do this, I think SQL Container overrides all default values from the combobox and I can only see the values/items coming from the database.

I need in combobox:

SQL Item1
SQL Item2
​SQL Item3
​SQL Item4