Problem to use component renderer in my column grid

Hi guys,
I try to use componentRenderer for one column in my grid. I have followed the instruction like in “book of vaadin” (chapter 5.5.3)
but my code is in error in my Eclipse IDE. My code below :

gridComp.addColumn(new ComponentRenderer<>(ComposantT -> {
			if (ComposantT.getEpplSfam() == null) {
			return "";
			} else {
			return ComposantT.getEpplSfam();
			}
			})).setHeader(new Html("<b>Code Famille<b>"));

and the error is :

The method addColumn(ValueProvider<ComposantT,?>) in the type Grid<ComposantT> is not applicable for the arguments (new ComponentRenderer<>((<no type> ComposantT) -> {  if ((ComposantT.getEpplSfam() == null))
  {
    return "";
  }  else
  {
    return ComposantT.getEpplSfam();
  }}))

But I have an other error :
Cannot infer type arguments for ComponentRenderer<>

Can anyone help me please ? :slight_smile:

I think the issue probably is that you are trying to render a string, whereas ComponentRenderer renders components (as its name implies). For your purpose, you can use Vaadin’s label component like so:

gridComp.addColumn(new ComponentRenderer<>(ComposantT -> {
			if (ComposantT.getEpplSfam() == null) {
			return new Label("");
			} else {
			return new Label(ComposantT.getEpplSfam());
			}
			})).setHeader(new Html("<b>Code Famille<b>"));

And naturally, you’ll need to import com.vaadin.flow.component.html.Label

This is very likely to be the correct solution to OPs issue, however I wanted to mention that Label is based on the <label> element, and should therefore only be used for form field labels.

Use Span or Text instead to simply add some text to a layout.

gridComp.addColumn(new ComponentRenderer<>(ComposantT -> {
	if (ComposantT.getEpplSfam() == null) {
		return new Span("");
	} else {
		return new Span(ComposantT.getEpplSfam());
	}
})).setHeader(new Html("<b>Code Famille<b>"));

Thank you for your answer, I understand my mistake.

I try your code but I have an other error message in my IDE “Cannot infer type arguments for ComponentRenderer<>”.

Do you have an idea ?

Isn’t that the error you initially asked about?

This happens when it does not see the lambda returning a Component. Please check your lambda that it always returns something that extends Component.
You could also define the type parameters yourself (new ComponentRenderer<Component, ComposantT>(item -> ...)), then you’ll see better where exactly in your lambda it tries to return something that is not a Component.

Thank you for your quick answer. But I’m sorry, I don’t understand what you mean.

Please see my edits

Another option is of course to not use a ComponentRenderer in the first place, if you want to display just a String anyway.

gridComp.addColumn(composantT -> {
	if (composantT.getEpplSfam() == null) {
		return "";
	} else {
		return composantT.getEpplSfam(); // assuming this returns a String..
	}
}).setHeader(new Html("<b>Code Famille<b>"));

I understand now, what you mean. I’ve defined the type parameters myself and it’s OK.

Thank you for your time and your quick answer another time.