ComboBox<Boolean> with caption


I am trying to add custom captions to a Boolean-ComboBox. Instead of true, false I want to display domain-specific terms, like “is active”, “not active” etc.

The only solution I found would be to implement a wrapper for Boolean which contains the caption text, then use an ItemCaptionGenerator. This I would have to do for each Boolean-Type field in my database.

This seems like mighty overkill for my intended purpose. Is there a simpler way, maybe similar to the old setCaption(Item, String)?


Something like that ?

       ComboBox<Boolean> c = new ComboBox<Boolean>();
       c.setItems(true, false);
       c.setItemCaptionGenerator(bool -> {return bool?"Active":"Inactive";});

Hello Jean-Christophe,

exactly like that. I really have to read up on those lambda expressions I guess.

Thank you very much!

It can be shortened even more:

ComboBox<Boolean> c = new ComboBox<>();
c.setItems(true, false);
c.setItemCaptionGenerator(bool -> bool?"Active":"Inactive");