javavaadinvaadin-flowvaadin21

How can I display checkboxes instead of boolean in vaadin grid?


I'm working on a new application using vaadin-spring-boot-starter 21.0.7 and trying to render training database information in a Grid. I'd like to have the first column in the list be a checkbox for Active/Inactive, but I can't figure out how to get that done. Everything I've seen has been for Vaadin 7 or 8. Mostly current code is in MainView.java at https://github.com/gregoryleblanc/ealOperators/tree/vaadin


Solution

  • You can do it like this for Vaadin 14 and newer. I have no experience with older versions of Vaadin.

    grid.addComponentColumn(myBean -> {
      Checkbox checkbox = new Checkbox();
      checkbox.setValue(true); // this you must handle
      checkbox.addClickListener(e -> {
        // todo
      }
      return checkbox;
    });
    

    The lambda may be replaced with a method reference

    grid.addComponentColumn(this::createCheckboxComponent);

    private Component createCheckboxComponent(MyBean myBean) {
      Checkbox checkbox = new Checkbox();
      // code here
      return checkbox;
    }