I am using Vaadin 14.1.21 + Java JDK/JRE 1.8
I have a single-select radiobuttongroup with a boolean field behind it in the DataClass.
I don't want to display "true" / "false" as options for the user, but custom Strings, let's say "red option" for true and "green option" for false.
I see in the API that there is a function called setRenderer, but I am not sure how to use it. I already figured out how to do it in a grid, but not on the selection box...
Selection box (todo):
rbgroup = new RadioButtonGroup<>();
rbgroup.setRequired(true);
rbgroup.setLabel("Title of radiobuttongroup");
rbgroup.setItems(true, false); // how to display them in the frontend as "Green option" / "Red option"?
....
binder.forField(gbgroup).bind("mybooleanField");
Grid: (done)
addColumn((mn) -> { // custom display
final String displayAS;
if (mn.getBoolValue()) {
displayAS = "Option Red";
} else {
displayAS = "Option Green";
}
return displayAS;
})
.setHeader("Columntitle")
.setFlexGrow(1)
.setSortable(true)
.setKey("Color Option");
As is shown in the code examples of RadioButtonGroup, setting a renderer to show text can be done like this (mix between example code and your code):
RadioButtonGroup<Boolean> rbgroup = new RadioButtonGroup<>();
rbgroup.setRequired(true);
rbgroup.setLabel("Title of radiobuttongroup");
rbgroup.setItems(true, false);
rbgroup.setRenderer(new TextRenderer<>((mn) -> {
if (mn) {
return "Option Red";
} else {
return "Option Green";
}
}));
If you want to display any Vaadin Component instead of only Text, you can use a ComponentRenderer
instead of a TextRenderer
. I use a simple Span
as example but you could build complex Layouts and return those instead.
RadioButtonGroup<Boolean> rbgroup = new RadioButtonGroup<>();
rbgroup.setRequired(true);
rbgroup.setLabel("Title of radiobuttongroup");
rbgroup.setItems(true, false);
rbgroup.setRenderer(new ComponentRenderer<>((mn) -> {
if (mn) {
return new Span("Option Green");
} else {
return new Span("Option Red");
}
}));