Container - why no 'void addItem(Item item)'?

It’s always struck me as odd that an Item cannot be passed to a container to add it.

Right now a typical flow when adding items to a container is:

  1. call addItem
  2. blank item is added to the container
  3. ItemSetChangeEvent is fired for the blank item
  4. edit item
  5. ValueChangeEvents are fired for the item properties

This is a little awkward as steps 2,3 cause components bound to the container to show an empty row because no data has been provided until step 4. This works ok when doing spreadsheet style editing of a table, but doesn’t work well when adding items through a seperate form.

To get round this, I tend to implement a method with signature: void addItem(Item item) This way an already populated new item can be added to the container in one operation.

Is there any reason why addItem(Item item) is not already part of the Container interface? Is there a better approach?

Did you try BeanContainer? Its been designed for that purpose only.

I’ve tried it, but I’m working on implementing a lazy container, and don’t want to use beans with reflection anyway. Prefer to implement the models for the views using the databinding api directly.

I think the underlying problem is one of abstraction - in general a container cannot just accept any Item, it must be responsible for creating the instance itself. I suppose there could be a “Container.createItem” method that could then be initialized before actually adding it to the container.