Issue adding items to Table

Guys, I have a problem with adding rows to a table.

The table has been created using:

        Table rightTable = new Table();
        rightTable.setSizeFull();
        rightTable.setPageLength(7);

        rightTable.setColumnReorderingAllowed(false);
        rightTable.setColumnCollapsingAllowed(true);

        rightTable.setSelectable(true);
        rightTable.setMultiSelect(true);
        rightTable.setImmediate(true); // react at once when something is selected
        
        rightTable.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN);
        rightTable.addContainerProperty("name", String.class,  null);
        //rightTable.setColumnWidth("name", 150);
        rightTable.setColumnAlignment("name", Table.ALIGN_LEFT);
        
        rightTable.addContainerProperty("info", Button.class,  null);
        rightTable.setColumnWidth("info", 20);
        rightTable.setColumnAlignment("info", Table.ALIGN_LEFT);

The table renders fine. And any rows I add to the table before its render add fine. However I have a button which is called:

        BeanItem<Role> newBeanRole = new BeanItem<Role>(role);
        rightTable.addItem(newBeanRole);
        rightTable.getContainerProperty(newBeanRole, "name").setValue(roleName);
        rightTable.getContainerProperty(newBeanRole, "info").setValue(infoButton);

Now this code works fine if the table has atleast one row in it before the button is pushed. However if the table is empty, the row NEVER appears in the table. However if there is already one or more rows, it gets displayed just fine. Using addItem instead of the code above gives me the same result.

I’ve tried calling rightTable.requestRepaint() and that didn’t work.

Is this a bug? Or have I missed something?

BTW, the printout at my console when the table is empty shows:
13:36:02.140 [http-8080-1]
DEBUG c.c.a.p.o.GrantEntitlementsPanel - Adding role Owner to selected roles
13:36:02.141 [http-8080-1]
DEBUG c.c.a.p.o.GrantEntitlementsPanel - Visible col[0]
: name
13:36:02.141 [http-8080-1]
DEBUG c.c.a.p.o.GrantEntitlementsPanel - Visible col[1]
: info
13:36:02.141 [http-8080-1]
DEBUG c.c.a.p.o.GrantEntitlementsPanel - Right table size before: 0
13:36:02.142 [http-8080-1]
DEBUG c.c.a.p.o.GrantEntitlementsPanel - Right table size after: 1

The addItem() is a bulder method that asks the container (or table in this case) to create a new item. The parameter is an identifier of the item, not the item itself. (Well OK, the item IDs are used in some components much like a items would, which can be a bit confusing.)

Table uses an IndexedContainer by default, so you can’t have BeanItems in it. You should instead use a BeanItemContainer, as documented
here
, and bind it to the table with setContainerDatasource(), or in the constructor of the table.

I’m not sure how that helps me. I’m trying to add an object to a table. There is NO datasource. The row I’m trying to add is a name, and a link button. The way I read the docs, if I use BeanItemContainer, then I can’t then have the button in the table.(Reading last part of section 9.4.2) I was just storing an object in the id as thats what I need if the data is selected. I’m not displaying anything other than a name from the object.

I’m confused as to why I can add objects to the table fine, and retrieve AS LONG as there is atleast one item in the table. Ie, as long as I add atleast one item to the table at creation, then the button push will successfully add more to eternity. I also added a delete button. If I add a few and then remove all, I can no longer add anymore. Surely this has nothing to do with what I’m putting in the ID column???

There is always a datasource - if you don’t set it explicitly, the table creates an IndexedContainer that it uses as its datasource. The add*() etc. methods in the table simply forward the requests to its datasource. You cannot add BeanItems into an IndexedContainer etc. - there is no standard container into which you could directly add items or arbitrary types.

With BeanContainer/BeanItemContainer, you could have a bean that contains the button (probably not what you want) or you could use a generated column in the table to create it.

In theory, you could also use a custom property that gets added to your BeanItems and maybe even handles the button creation and caching on the fly, but that can get rather complicated.

Probably related to Marko’s earlier observations about item identifiers vs. items - maybe you are effectively using a BeanItem as the item identifier for an empty IndexedContainerItem or something like that.

Ok, so I’ve gone to using an Integer as my itemID. Still doesn’t work.

However when I stop using setColumnWidth it all works. Once I set the column width again, it doesn’t work… this is using vaadin 6.7.0-rc1

Hi Paul,

Thank God you found out this setColumnWidth bug… I was going insane with it! It happens to me to. Temporarily I am using setColumnExpandRatio to solve it.

Regards
Bruno Santos

Created http://dev.vaadin.com/ticket/7731 for this