Loading...
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.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

CollectionContainer - bind arrays and collections

Sami Ekblad
1 decade ago Feb 21, 2010 11:02am
Sami Ekblad
1 decade ago Apr 15, 2011 6:23pm

Hi all,

I was working on a small analysis project and along the way I found a nifty way of displaying CSV (comma separated values) in Vaadin Table.

To do this you need OpenCSV and the CollectionContainer add-on.

But with these it becomes quite easy:

// Read using OpenCSV
CSVReader r = new CSVReader(new FileReader(file));
List<Object[]> rows = r.readAll();

// Extract column captions
String[] columnHeaders = (String[]) rows.get(0);
rows.remove(0); 

// Bind to table
CollectionContainer ds = CollectionContainer.fromPrimitives(rows, -1);
table.setContainerDataSource(ds);
table.setColumnHeaders(columnHeaders);

Disclaimer: As you can see here, the data is read fully into a List (which does not scale to large amounts) and no error checking is present (left it out for readability).

Anyway, this works very well for my case of "typical" amount of data (i.e. ~1000 rows, ~10 columns).

Frans Flippo
8 years ago Oct 10, 2013 2:09pm