Collecting items into an IndexedContainer in different ways

Hi All, I’m a newer with Vaadin and i develop an application with this framework. But i have some problems that i can’t only resolve looking at the book or consulting the forum.
I’m working in different ways to add items into a IndexedContainer. Until now, I have nested beans like User, Role, etc… and i added items like this:

//Set properties (Columns: Names and types) IndexedContainer ic = new IndexedContainer();
ic.addContainerProperty(“id”, Integer.class, null);
ic.addContainerProperty(“name”, String.class, null);
ic.addContainerProperty(“password”, String.class, null); ic.addContainerProperty(“email”, String.class, null);

…some code…

final User u = userIterator.next();
final Item item = ic.addItem(u);
item.getItemProperty(“id”).setValue(counter++);
item.getItemProperty(“name”).setValue(u.getUsername());
item.getItemProperty(“password”).setValue(u.getPassword());
item.getItemProperty(“email”).setValue(u.getEmail());

But now i’m not in this situation. I have to build an IndexedContainer (and a table with this) with columns that not are the same in all situations.
I looked for a solution and i found this:

IndexedContainer ic = new IndexedContainer();
Iterator iterColumns = resultsTable.getColumns().iterator(); //Iterator of Strings with the name of the columns
ic.addContainerProperty(iterColumns.next(), String.class, null);

…some code…

//Iterate row items
Iterator iterRows = resultsTable.getTableData().iterator();
PropertysetItem item = new PropertysetItem(); //i saw that in an example
List rowElements = iterRows.next();

…some code…

Object element = rowElementsIterator.next();
item.addItemProperty(element.getColumnName(), new ObjectProperty(element.getValue().toString()));

when i try to add the item with “ic.addItem(item);” It returns null, null, null … could somebody help me with this issue? thank you all, Manu.

I’m not sure if I understood your question correctly, but I’ll try to help. First of all you should have all the necessary columns in your Container and then control the visibility of the columns in your Table with Table.setVisibleColumns(). Creating a container from Table contents is quite backwards.
Another issue I notice is that perhaps you misunderstood the Container.addItem(Object) method wrong. Calling IndexedContainer.addItem(Object) is not adding the item you give as a parameter, but instead it creates a new Item internally of type IndexedContainerItem and adds it to the container and then returns the item. The Object parameter to addItem is the id of your newly added item.

I need to create an IndexedContainer and then, give it to the Table. The method that creates the IndexedContainer must be prepared for many situations so the number of the properties are diferent.
I can create a PropertysetItem with all the properties and values correctly. But, when i try to add it to the container with addItem(item) this method returns null null…
I understant what you are explaining me but i don’t know what is the correct way to add an item in this situation.

The specification says:


Item com.vaadin.data.util.IndexedContainer.addItem(Object itemId)

Creates a new Item with the given ID in the Container.
The new Item is returned, and it is ready to have its Properties modified. Returns null if the operation fails or the Container already contains a Item with the given ID.

thanks for answer me Johannes.

IndexedContainerItem doesn’t support adding properties. The properties must be added to the IndexedContainer i.e. all items share the same set of properties.

There is no standard container that would let you create your own items and then add them to the container. Instead, all the containers that come with Vaadin create their own items - e.g. BeanItemContainer and BeanContainer use BeanItem, IndexedContainer actually stores the data in the container and IndexedContainerItem just fetches it from there at runtime, SQLContainer uses RowItem etc. The addItem(Object) method takes an item ID, never a pre-constructed Item.

The comment about being ready to have its Properties modified refers to the values of properties, not the set of available properties.

A container needs to have a set of container properties, which all its items must have. However, the items in the container could technically have additional properties, although this has not really been tested by the core development team. It should not be too hard to subclass AbstractInMemoryContainer to create your own container that accepts arbitrary items if you really need such a container.