Search code examples
javavaadinvaadin8

how to put css styling for one specific checkbox in checkboxGroup of vaadin 8


I have a checkboxgroup which has multiple checkboxes. I have certain checkboxes which needs to look different, either by bold text or colored text ,which wont change irrespective of selection/unselection. I have following code to build checkboxgroup. But I am not able to put style specific to one checkbox, because I dont have access to it. How can I do that

CheckBoxGroup<ReferenceScreenResultAnswer> answersOptionGroup = new CheckBoxGroup<>(question.getText());
List<ReferenceScreenResultAnswer> checkBoxItems = new ArrayList<>();
answersOptionGroup.setItems(checkBoxItems);
.......
// this is where i want to put CSS to specific checkbox/values
for (Answer answer : preSelectedAnswer)
{
    ReferenceScreenResultAnswer rsra = new ReferenceScreenResultAnswer();
    rsra.setAnswer(answer);
    rsra.setReferenceScreenResultQuestion(rsrq);
    answersOptionGroup.select(rsra);
}

I can do invidiual checkboxes like

CheckBox cb = new CheckBox();
cb.setCaptionAsHtml(true);
cb.setCaption("<b> hello </b> there");

But I am not able to access individual checkboxes from CheckBoxGroup. Any idea how to access them


Solution

  • i found the answer:

                        // css style the pre selected answer, so they look different irrespective
                        // of their selection
                        answersOptionGroup.setItemCaptionGenerator(new ItemCaptionGenerator<ReferenceScreenResultAnswer>()
                        {
                            @Override
                            public String apply(ReferenceScreenResultAnswer item)
                            {       
                                if (preSelectedAnswer.contains(item.getAnswer()))
                                    return "<strong>" + item.getAnswer().toString() + "</strong>";
                                else
                                    return item.getAnswer().toString();
                            }
                        });