Java wanna bee
Java wanna bee

Reputation: 3107

Vaadin 14 Error: Cannot resolve constructor com.vaadin.flow.data.renderer.ComponentRenderer<>

I'm trying to iterate over a HashMap to display the content in Vaadin14 Grid to look like this:

enter image description here

that's the java class:

public class MyClass extends VerticalLayout {

    // Dummy Data
    LocalDate date1 = LocalDate.now();
    LocalDate date2 = LocalDate.now();
    LocalDate date3 = LocalDate.now();
    Boolean[] isPresent1 = {true, false, false, true, true, false, false, true, true};
    Boolean[] isPresent2 = {true, false, false, true, true, false, false, true, true};
    Boolean[] isPresent3 = {true, false, false, true, true, false, false, true, true};

    Map<LocalDate, Boolean[]> trainingsMap = new HashMap<>();

    Grid<Map.Entry<LocalDate, Boolean[]>> grid = new Grid<>();

    Icon icon;

    public MyClass() {
        // in reality data come from Spring Data - Repository - Service
        trainingsMap.put(date1, isPresent1);
        trainingsMap.put(date2, isPresent2);
        trainingsMap.put(date3, isPresent3);

        for (Map.Entry<LocalDate, Boolean[]> map : trainingsMap.entrySet()) {
            grid.addColumn(new ComponentRenderer<Component, Map<LocalDate, Boolean[]>>(createIsPresent(map.getValue())));
                   // .setHeaderRenderer(new LocalDateTimeRenderer<>(map.getKey(), "dd/MM HH:mm:ss"))); // 
        }

        grid.setItems(trainingsMap.entrySet());
        add(grid);
    }

    private Component createIsPresent(Boolean[] isPresent) {
        for (Boolean b : isPresent) {
            if (b) {
                icon = UIUtils.createPrimaryIcon(VaadinIcon.CHECK);
            }
        }
        return icon;
    }

}

Error: Cannot resolve constructor com.vaadin.flow.data.renderer.ComponentRenderer<>

Is this possible at all or am I doing something fundamentally wrong?

Upvotes: 0

Views: 786

Answers (2)

Saurabh Rathore
Saurabh Rathore

Reputation: 11

The correct implementation for this would be like below.

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.renderer.ComponentRenderer;

public class MyClass extends VerticalLayout {

    // Dummy Data
    LocalDate date1 = LocalDate.now();
    LocalDate date2 = LocalDate.now();
    LocalDate date3 = LocalDate.now();
    Boolean[] isPresent1 = {true, false, false, true, true, false, false, true, true};
    Boolean[] isPresent2 = {true, false, false, true, true, false, false, true, true};
    Boolean[] isPresent3 = {true, false, false, true, true, false, false, true, true};

    Map<LocalDate, Boolean[]> trainingsMap = new HashMap<>();

    Grid<Map.Entry<LocalDate, Boolean[]>> grid = new Grid<>();

    Icon icon;

    public MyClass() {
        // in reality data come from Spring Data - Repository - Service
        trainingsMap.put(date1, isPresent1);
        trainingsMap.put(date2, isPresent2);
        trainingsMap.put(date3, isPresent3);
        grid.setItems(trainingsMap.entrySet());
        grid.addColumn(new ComponentRenderer<>(item -> createIsPresent(item.getValue())));
        add(grid);
    }

    private Component createIsPresent(Boolean[] isPresent) {
        for (Boolean b : isPresent) {
            if (b) {
                icon = UIUtils.createPrimaryIcon(VaadinIcon.CHECK);
            }
        }
        return icon;
    }

}

Upvotes: 0

anasmi
anasmi

Reputation: 2652

The error states that there is no available constructor with parameters you've supplied. And, otherwise, you can't pass a method call as a parameter in Java. What you can do is to pass a method reference.

Some examples could be found here:

ComponentRender's available constructors are here: Component Renderer constructors. You could use constructor with a SerializableSupplier,for example, if it suits you better.

There are multiple examples at the official documentation page on how to use a Renderer employing different constructors: Using Component Renderers

Quoting from there:

Example: Using ComponentRenderer with a Supplier.

grid.addColumn(
    new ComponentRenderer<>(() -> new Icon(VaadinIcon.ARROW_LEFT)));

So you would need to modify this line :

new ComponentRenderer<Component, Map<LocalDate, Boolean[]>>(createIsPresent(map.getValue()))

Hope it helps : )

Upvotes: 1

Related Questions