Search code examples
vaadinvaadin7vaadin-grid

How to display checkboxes instead of boolean in vaadin grid?


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 ?


Solution

  • 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())