Coloring cells in Vaadin Grid that uses OpenCSV

Issue

I am attempting conditional coloring/formatting for a Vaadin Grid that sources its data from a file (using OpenCSV) rather than a POJO. I was looking at Change color of cell in Grid (Vaadin) to see if this could be modified to support CSV instead, but have so far come up empty.

Can this be done with the cell style generator method? I am referencing https://vaadin.com/blog/read-and-display-a-csv-file-in-java for the reading of the csv data.

Solution

Going off the CSVReader and Vaadin documentation examples with the generic Person class, you can do something like:

grid.addColumn(str->str[columnIndex])
.setKey(header)
.setHeader(header)
.setClassNameGenerator(str -> {
    if (Integer.valueOf(str[columnIndex]) > 8){
        return "condition1";
    }
                            
    if (Integer.valueOf(str[columnIndex]) < 8){
        return "condition2";
    }
                            
    return null;
});

Similar to what is done in the cookbook example, you can then define those conditions in the corresponding css file.

Answered By – schaphekar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published