Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
SQLContainer iteration
Hi:
How do I iterate over a SQLContainer, for example, to add all the data in one column to a string....
regards,
Hugo
Hi,
SQLContainer implements the IndexedContainer interface, so you can do something like this:
for (int i = 0; i < container.size(); i++) {
Object id = container.getIdByIndex(i);
Item item = container.getItem(id);
// do stuff with item
}
The only thing to remember is to avoid the getItemIds() method if at all possible, since this method will load all rows from the database, which can be very slow if you have a huge dataset.
Or if you mean to display additional computed cells in a table, you should check out Table.addGeneratedColumn().
HTH,
/Jonatan
Jonatan Kronqvist: Hi,
SQLContainer implements the IndexedContainer interface, so you can do something like this:
for (int i = 0; i < container.size(); i++) { Object id = container.getIdByIndex(i); Item item = container.getItem(id); // do stuff with item }
The only thing to remember is to avoid the getItemIds() method if at all possible, since this method will load all rows from the database, which can be very slow if you have a huge dataset.
Or if you mean to display additional computed cells in a table, you should check out Table.addGeneratedColumn().
HTH,
/Jonatan
Thank you very much Jonatan, I was actually using getItemIds(), so you help a lot with your answer, which worked perfect...
regards,
Hugo
Jonatan Kronqvist: Hi,
SQLContainer implements the IndexedContainer interface, so you can do something like this:
for (int i = 0; i < container.size(); i++) { Object id = container.getIdByIndex(i); Item item = container.getItem(id); // do stuff with item }
The only thing to remember is to avoid the getItemIds() method if at all possible, since this method will load all rows from the database, which can be very slow if you have a huge dataset.
Or if you mean to display additional computed cells in a table, you should check out Table.addGeneratedColumn().
HTH,
/Jonatan
Jonatan:
I have a different problem now, and I'm not sure if I this is the right thread, but is that the getIdByIndex(i) method doesn't work for hierarchical containers...
I'm try this now with a treetable (tree):
for (int i = 0; i < tree.getContainerDataSource().size(); i++) {
Item item = tree.getContainerDataSource().getItem(tree.getContainerDataSource().getItemIds());
Object value =item.getItemProperty(PROPERTY).getValue();
}
...but it gives me a null pointer exception error....I'm not sure how to fix this...any idea??
regards and ,as always, thank you very much....
Hugo
Sorry for the late reply, but I don't really understand the code you posted. You talk about getIdByIndex(), but don't call it anywhere. Instead you have a call to getItem(getItemIds()), which I'm sure will return NULL, since getItemIds() will return a collection and not an ID for any item.
Anyway, I guess you already solved the problem :)
/Jonatan