Vaadin 14错误:无法解析构造函数com.vaadin.flow.data.renderer.ComponentRenderer <>

时间:2019-08-17 18:29:57

标签: hashmap vaadin vaadin-grid vaadin-flow

我正在尝试遍历HashMap以在Vaadin14 Grid中显示内容,如下所示:

enter image description here

那是java类:

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;
    }

}
  

错误:无法解析构造函数com.vaadin.flow.data.renderer.ComponentRenderer <>

这完全有可能还是我做错了什么?

2 个答案:

答案 0 :(得分:1)

错误表明您提供的参数没有可用的构造函数。而且,否则,您不能在Java中将方法调用作为参数传递。您可以做的是传递方法引用。

可以在此处找到一些示例:

ComponentRender的可用构造函数在这里: Component Renderer constructors。例如,如果更适合您,可以将其与SerializableSupplier一起使用。

官方文档页面上有多个示例,说明如何使用采用不同构造函数的Renderer: Using Component Renderers

从那里报价:

  

示例:与供应商一起使用ComponentRenderer。

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

所以您需要修改此行:

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

希望它会有所帮助:)

答案 1 :(得分:0)

正确的实现方式如下。

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;
    }

}