Remove Icon in a Datagrid

Hello all together,

I have a problem with the IconRenderer on a datagrid.
I created a Datagrid with a column for icons with iconRenderer.

 <column id="icon" generatedType="com.haulmont.cuba.gui.icons.Icons$Icon">
        <iconRenderer/>
  </column>

Also I genereted a ColumnGenerator for this column.

@Install(to = "positionenDataGrid.icon", subject = "columnGenerator")
private Icons.Icon positionenDataGridIconColumnGenerator(DataGrid.ColumnGeneratorEvent<ObjektPosition> event) {
    return isNewAdded ? CubaIcon.ENABLE_EDITING : null
}

This Generator should return an icon when the var “isNewAdded” is true and if it is not, then it should remove the icon. When initializing the screen all icons are set as they were expected.

If afterwards the variable “isNewAdded” changes from true to false, then the icon simply remains.

I have tried a repaint the table without success. I have then replaced the “null” value with another icon.

@Install(to = "positionenDataGrid.icon", subject = "columnGenerator")
private Icons.Icon positionenDataGridIconColumnGenerator(DataGrid.ColumnGeneratorEvent<ObjektPosition> event) {
    return isNewAdded ? CubaIcon.ENABLE_EDITING : CubaIcon.LOOKUP_OK;
}

This worked fine. With each change of the variable either the first or the second icon appeared

Does anyone have an idea what the reason is or how to solve the Poblem could.

Thanks a lot for any help!

Hi @nikolaos.batzakakis

The issue was reproduced. The cause of the problem is that the Vaadin does not update the value correctly.

Here are two workarounds to solve the problem. Let’s say the isNewAdded variable updates after clicking the button with iconBtn id:

  1. The column could be generated programmatically without defining in the descriptor and re-added each time after clicking the button:
private void addColumn(){
    DataGrid.IconRenderer newEntityIconRenderer = newEntitiesTable.createRenderer(DataGrid.IconRenderer.class);
    DataGrid.Column iconColumn = newEntitiesTable.addGeneratedColumn("icon", new DataGrid.ColumnGenerator<NewEntity, Icons.Icon>() {

        @Override
        public Icons.Icon getValue(DataGrid.ColumnGeneratorEvent<NewEntity> event) {
            return isNewAdded ? CubaIcon.ENABLE_EDITING : null;
        }

        @Override
        public Class<Icons.Icon> getType() {
            return Icons.Icon.class;
        }
    });
    iconColumn.setRenderer(newEntityIconRenderer);
    iconColumn.setCaption("Icon column");
}

@Subscribe
public void onBeforeShow(BeforeShowEvent event) {
    addColumn();
}

@Subscribe("iconBtn")
public void onIconBtnClick(Button.ClickEvent event) {
    isNewAdded = !isNewAdded;
    newEntitiesTable.removeColumn("icon");
    addColumn();
}
  1. You can use the Label component with added icon instead of Icon directly:
@Inject
private DataGrid<NewEntity> newEntitiesTable;

private boolean isNewAdded = true;
@Inject
private UiComponents uiComponents;

@Subscribe("iconBtn")
public void onIconBtnClick(Button.ClickEvent event) {
    isNewAdded = !isNewAdded;
    newEntitiesTable.repaint();
}

private Label getLabelWithIcon(){
   Label label = uiComponents.create(Label.class);
   label.setIcon("font-icon:PENCIL");
   return label;
}

@Install(to = "newEntitiesTable.icon1", subject = "columnGenerator")
private Component newEntitiesTableIcon1ColumnGenerator(DataGrid.ColumnGeneratorEvent<NewEntity> event) {
    return isNewAdded ? getLabelWithIcon() : uiComponents.create(Label.class);
}

In this way you should add the ComponentGenerator in the descriptor:

<column id="icon1" generatedType="com.haulmont.cuba.gui.components.Component" caption="Label icon">
  <componentRenderer/>
</column>

I hope these recommendations will be helpful.

Regards,
Nadezhda.

Thank you Nadezhda for your workaround, that worked great!! I used the second solution for my Project.