7

I'm trying to display my boolean values as a checkbox in a vaadin grid. I can't use the multi selection mode because i need two columns with checkboxes. The columns of the Checkboxes shell have a Caption but the Checkboxes itself shell be without a caption. Does anyone have an idea ?

julianspaeth
  • 205
  • 1
  • 3
  • 9

3 Answers3

6

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!"));
Piotrowy
  • 605
  • 8
  • 20
5

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.

Reto Höhener
  • 5,419
  • 4
  • 39
  • 79
  • Thanks but i need also to check and uncheck the boxes – julianspaeth Nov 29 '16 at 16:15
  • 1
    @spaethju below I put answer which meets your requirements. – Piotrowy Dec 28 '16 at 23:51
  • I got the error "The converter model type class java.lang.Boolean is not compatible with the property type class java.lang.String (in Column[propertyId:role1])" when I tried the above. Did anyone ever get it to work? – Tony B Nov 19 '18 at 04:08
  • @user3329922 you are providing a string, but the converter expects a boolean. – Reto Höhener Nov 19 '18 at 08:49
  • Yes, the error is easy to understand. What I don't understand is how the original idea in this post even worked, since it should have the same problem. So, as I asked before, did this idea work for people? – Tony B Nov 19 '18 at 17:38
  • @user3329922 So, as I stated in the first line of my answer, this worked for me. The error seems to indicate that your grid model returns a string value for the column that you try to render as a checkbox. You either need to set the converter on the correct (boolean) column, or change your model to provide a boolean instead of a string value. – Reto Höhener Nov 19 '18 at 19:04
  • ahh, so maybe it is because we have a Bean method "getRole1" which returns string and a bean method "isRole1" which returns Boolean, and the converter is getting confused. They both refer to the same underlying data ( which does happen to be string ). Normally in these cases having a isRole1 works, but maybe I need to experiment with my Bean method naming, see if I can get around it. – Tony B Nov 19 '18 at 19:16
  • @user3329922 you could slightly change the converter to `Converter`, and parse `"true"` or `"1"` or whatever your bean delivers as a boolean. Or add a getter that does that conversion and display that in the column. – Reto Höhener Nov 19 '18 at 19:53
2

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())
Krzysztof Majewski
  • 2,494
  • 4
  • 27
  • 51