Changing the background color of a cell in Grid

Hello everybody,

I am using the basic grid from the documentation:

List<Person> people = Arrays.asList(
        new Person("Nicolaus Copernicus", 1543),
        new Person("Galileo Galilei", 1564),
        new Person("Johannes Kepler", 1571));

// Create a grid bound to the list
Grid<Person> grid = new Grid<>();
grid.setItems(people);
grid.addColumn(Person::getName).setHeader("Name");
grid.addColumn(person -> Integer.toString(
               person.getYearOfBirth()))
        .setHeader("Year of birth");

layout.add(grid);

I would like to change the background of the cells where the year is >= 1564. I saw that I could use TemplateRenderer but it renders every entry in the column. How can I specify a cell and change its background?

I will be grateful for any help.

You can use addClassNameGenerator(…) in the column and conditionally add a class name in that column.

See example here: https://github.com/TatuLund/devday-demo-flow/blob/master/src/main/java/com/vaadin/devday/demo/views/GridView.java#L137

And how to use it in styles: https://github.com/TatuLund/devday-demo-flow/blob/master/src/main/webapp/frontend/styles.html#L215