How to display checkboxes instead of boolean in vaadin grid?

余生长醉 提交于 2019-12-04 08:48:02

You have to add generated columns for your checkboxes

    GeneratedPropertyContainer gpcontainer = new GeneratedPropertyContainer(container);
    gpcontainer.addGeneratedProperty("columnName",
        new PropertyValueGenerator<CheckBox>() {
        @Override
        public CheckBox getValue(Item item, Object itemId,
                                Object propertyId) {

            // set checkBox listener etc. in here
            return new CheckBox();
        }

        @Override
        public Class<CheckBox> getType() {
            return CheckBox.class;
        }
    });

Grid grid = new Grid(gpcontainer);

You can find more detailed example in here in section "GeneratedPropertyContainer"

https://vaadin.com/docs/-/part/framework/datamodel/datamodel-container.html#datamodel.container.gpc

EDIT: Also set `ComponentRenderer' for your column

mainGrid.addColumn(COLUMN).setRenderer(new ComponentRenderer())

I suggest to use this repo https://github.com/vaadin/grid-renderers-collection-addon. This project is developed by Vaadin devs and it provides CheckboxRenderer class. You can see it in demo but using it is very simple.

First you have to add repository and dependency into your project. In maven it looks like this:

...
<repositories>
    ...
    <repository>
        <id>vaadin-addons</id>
        <url>http://maven.vaadin.com/vaadin-addons</url>
    </repository>
</repositories>
...
<dependencies>
    ...
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>grid-renderers-collection-addon</artifactId>
        <version>0.94</version>
    </dependency>
</dependencies>
...

Then you can use it like this:

grid.getColumn(columnName).setRenderer(new CheckboxRenderer());

You can also easily add listener:

CheckboxRenderer renderer = new CheckboxRenderer();
grid.getColumn(columnName).setRenderer(renderer);
grid.getColumn(columnName).setHeaderCaption("");
renderer.addClickListener(e -> System.out.println("Hello listener!"));

If you only need a read-only checkbox, this worked for me:

grid.getColumn("columnId").setRenderer(new HtmlRenderer(), new BooleanConverter());

HtmlRenderer is provided by the Vaadin framework, and the boolean converter looks like this:

public class BooleanConverter implements Converter<String, Boolean>
{   
  @Override
  public Boolean convertToModel(String value, Class<? extends Boolean> targetType, Locale locale) throws ConversionException
  {
    return null;
  }

  @Override
  public String convertToPresentation(Boolean value, Class<? extends String> targetType, Locale locale) throws ConversionException
  {
    return "<input type='checkbox' disabled='disabled'" + (value.booleanValue() ? " checked" : "") + " />";
  }

  @Override
  public Class<Boolean> getModelType()
  {
    return Boolean.class;
  }

  @Override
  public Class<String> getPresentationType()
  {
    return String.class;
  }
}

This will give you a native browser checkbox, however, not a Vaadin CheckBox.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!