How to sort a ComboBox?

Hi,

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.

Given:


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

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?

Thanks,

Eric

Hi,

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?

Thanks,

Eric

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

example:
ArrayList listOfTheElements=new ArrayList… put here the elements.

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

while(iterator.hasNext()){
cities.addItem(iterator.next());

}

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,

Eric

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[]
{true}) 

but it throws an exception:


java.lang.UnsupportedOperationException: Indexed container item does not support adding new properties
	com.vaadin.data.util.IndexedContainer$IndexedContainerItem.addItemProperty(IndexedContainer.java:771)
	com.example.vaadintutorial.ui.PersonForm.<init>(PersonForm.java:55)
	com.example.vaadintutorial.VaadintutorialApplication.getListView(VaadintutorialApplication.java:73)
	com.example.vaadintutorial.VaadintutorialApplication.init(VaadintutorialApplication.java:82)
...

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?

Thanks!

Eric

/* 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.addItem(2).getItemProperty("name").setValue("Turku");
cityContainer.addItem(3).getItemProperty("name").setValue("Paris");
cityContainer.addItem(4).getItemProperty("name").setValue("Zanzibar");
cityContainer.addItem(5).getItemProperty("name").setValue("Turin");
cityContainer.addItem(6).getItemProperty("name").setValue("London");
cityContainer.getItem(cityContainer.addItem()).getItemProperty("name").setValue("New York");
/* Lets sort the container on ascending name*/
cityContainer.sort(new Object[]{"name"}, new boolean[]
{true});

/* 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);
comboBox.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
comboBox.setItemCaptionPropertyId("name");

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

Thanks so much!

Eric

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:

None
SQL Item1
SQL Item2
​SQL Item3
​SQL Item4
​…