Vaadin Grid: List<String> as items possible?

Hi,

i am trying to show a list of String-Arrays in a grid. For example having the two arrays [a,b,c]
and [1,2,3]
in a list as data it should result in two rows and 3 columns

a|b|c
1|2|3

Is this possible with vaadin-grid? I don’t know how to implement the value provider or renderer. Somehow i need to reference my values by column index.

Thanks

Thomas Wagner

Yes, it’s possible, e.g. like this:

    private final Grid<List<String>> listGrid = new Grid<>();
    listGrid.addColumn(list -> list.get(0)).setHeader("First item of list");
    listGrid.addColumn(list -> list.get(1)).setHeader("Second item of list");

Thanks for your answer and sorry, i forgot an important information. The problem is that i don’t know the number of columns and have to do this in a loop.

	private final Grid<List<String>> listGrid = new Grid<>();
	for (int i = 0; i < numberOfColumns; i++) {
      listGrid.addColumn(list -> list.get(i)).setHeader("list item " + i);
	}

This does not compile, cause i is not final and can not used in the lambda expression.

There are many ways around that. For example, you don’t need to use the loop index there, you can use a final variable:


    for (int i = 0; i < numberOfColumns; i++) {
	    int finalI = i;
        listGrid.addColumn(list -> list.get(finalI)).setHeader("");

A good Java IDE should already hint you this kind of solution.

Thanks for your answer. I am a little bit ashamed, that i did not saw this solution myself. I am using eclipse as ide and don’t get any hints for this problem.