Hi folks,
maybe it is a silly question. I noticed that you can put components into a table. That’s a very useful thing. Because i want to put a button into my table. But i have a problem. In the example written in the VAADIN book the components are added directly to the table with
addItem(Object[] cells, Object itemId)
. But in my special case i use a container. And not Container has such a method to override it. So how can i use a container and put components in a table?
Thanks for your help
John here has a fine solution on how to add items to a container.
All addItem or modify items etc. that you find in tables, selects and other components are just in fact wrapper functions for the container - everything done in those functions are actually done by calling the container directly. Every AbstractSelect extending Component (ie. Tree, Table, Select) has always a container where the items are in, even if you don’t specify one.
If we look into the code of the function
public Object addItem(Object[] cells, Object itemId)
, we’ll see how the table interacts with the container. Here’s the most important part of that function, rows 1656-1674 of Table in version 6.0.0 of Vaadin:
// Creates new item
Item item;
if (itemId == null) {
itemId = items.addItem();
if (itemId == null) {
return null;
}
item = items.getItem(itemId);
} else {
item = items.addItem(itemId);
}
if (item == null) {
return null;
}
// Fills the item properties
for (int i = 0; i < availableCols.size(); i++) {
item.getItemProperty(availableCols.get(i)).setValue(cells[i]
);
}
the field items refers to the container in the table. There is a lot of failsafe checks in that function, to handle different input, but basically by doing this you will do exactly the same as this function
Item item = container.addItem(itemId);
Collection columns = container.getContainerPropertyIds();
for (int i = 0; i < columns.size(); i++) {
item.getItemProperty(columns.get(i)).setValue(cells[i]
);
}
Thank you for this quick solution. If I get it right, you mean, adding a component like a button, TextField, Label doesn’t need any special implementation. These components will be added automatically to the table intern container. Because in John’s solution i don’t get the clue where the components are. In the object array? And can an item handle components as properties? So they will be shown on the table?
You got that right, any object will do fine as an item property, including Components. Well, I wouldn’t go and try adding Table components into another Table, that’s probably not a good idea (although tested to work, if I remember correctly)
Note, that this goes only for the Table component, other AbstractSelect implementations do not support adding Components as items (restriction mainly on the client-side rendering, not on the Java API level).