Table Container: add existing com.vaadin.ui.Item to a container

Hello everyone,

in our Vaadin application, we have a custom system in place that manages the databinding. It basically maintains a hash map from each of our data objects to a custom implementation of
com.vaadin.data.Item
, and these items contain custom implementations of
com.vaadin.data.Property
.

This allows us to keep the UI in synch, as we simply bind these Properties to text fields, combo boxes and so on. However, one component refuses to play nice with this approach, and that’s the
com.vaadin.ui.Table
.

A Table uses a
com.vaadin.data.Container
as its model, and one may add elements using container.addItem(…). Okay. But the entire databinding relies on the fact that
for each data model element, there is exactly one
com.vaadin.data.Item. Otherwise there are two items and any two UI elements that actually use the same model element (but different items) will go out of synch.

So, how does the Vaadin API intend to deal with such cases? How can we re-use our existing com.vaadin.data.Item instances in the context of a Table or Container? The default Container implementations do not have an “addItem” method that really takes a com.vaadin.data.Item instead of an item ID.

To illustrate my point, here’s a real-world example. Imagine a typical “master detail” pattern on the UI. On the left side, there is a table that displays a list of all items (the list only shows the names), and if you click on an entry, the editor on the right displays its details. Now, if you edit the name of the item in the detail view, we want the entry in the table to update automatically. The thing is: we can’t use the com.vaadin.data.Item that is generated by the Table, because we already generated our own that we use everywhere else.

One more example: imagine you have multiple tables, displaying different subsets of your data. They can’t use the same container, but they must use the same com.vaadin.data.Item instances, otherwise they go out of synch.

Perhaps I’m just missing something in the Vaadin API? Any hints would be much appreciated.

Thanks,

Martin

An old question, but as this pops up every now and then:

Vaadin Container API does not prevent the use of an Item constructed elsewhere in the container, but none of the standard containers included in Vaadin support externally generated Items. The normal approach is to let the container create the items and get them from the container to be displayed on forms etc.

It should be very easy to create a container (extending AbstractInMemoryContainer) that lets you add your own pre-constructed Item instances, though. Two things to keep in mind if doing so: be careful not to introduce memory leaks through listeners if using the same Item (and Property) instances in multiple places, and make sure the new items contain all the properties that the container expects them to contain.