Table prepend item

I realize this is a n00b question and I’ve searched through the forums and documentation but could not find an answer to it.

I would like to “prepend” an item to the table instead of “append” it. Currently the method table.addItem appends the new item down there, but I would like something like table.append(Object, 0) where index 0 is the first table row or something along those lines.

Any insight appreciated.


Table’s default Container is IndexedContainer, you can just say:

((IndexedContainer) table.getContainerDataSource()).addItemAt(0, Object);


Thank you for your reply.

This is exactly what I was looking for but the second parameter “Object” is the itemID which you specify, in fact you can leave it out and the table will generate an itemID for you.

The other method table.additem(Object customRow, Object itemID) is what I’m trying to achieve, meaning that I want to prepend a custom object Object customRow and assign it a custom itemID … but I want this to be done at index 0.

The IndexedContainer method addItemAt does not allow me to specify the customRow object. Nevertheless, I tried the following:

Object[] myRow = {new customObject()}; // create a custom component spanning single column only

((IndexedContainer) myTable.getContainerDataSource()).addItemAt(0, myItemID); // This adds an empty item at 0

Item item = myTable.getItem(myItemID); // Retrieve the empty item just added
item.getItemProperty("mySingleColumn").setValue(myRow); // replace empty item with myRow object

…yet still this shows an empty table row and myRow object is not added.

Please advise. Thanks.


It should work exactly as you have done, except that the setValue() should set the object contained in the array, not the array itself. In that line you are setting just the one property, not all properties.

Using the array is useful if you have multiple table columns, as is done in the conveniency addItem() method in Table.

Hi Marko! Thank you for your prompt reply, and yes indeed since I was using only ONE column, I didn’t need the Object array but the inner custom component object itself to be the value of the table item. That surely did the trick!

Would this - in your opinion - be more efficient than using a table.sort operation which would produce the same result i.e. bringing the newly added table item to the top row (since sorting is by date added)? Just interested to know the difference in performance between the two, given that my table has a few rows (less than 30) and not a mega table with thousands of records.