Hello Everyone,
I’ve made a ‘Generic’ RadioButtonGroup for Yes/No selection and it return a ‘Y’ or ‘N’ value.
Sometimes when initializing the form, it’s possible that I do a setValue() with another value thant the ‘Y’ or ‘N’.
On the screen, nothing is selected and that is ok, but if I do a getSelectedItem(), it return the other value instead of null.
public static RadioButtonGroup<String> createYesNoField(Layout layout, String caption, String placeHolder, boolean readOnly) {
List <String> ynList = Arrays.asList(placeHolder.split("[|]
"));
RadioButtonGroup<String> rbS = new RadioButtonGroup<String>(caption, ynList);
rbS.setItemCaptionGenerator(item -> item.equals("N")?"NO":"YES");
rbS.addStyleName(ValoTheme.OPTIONGROUP_HORIZONTAL);
rbS.setReadOnly(readOnly);
layout.addComponent(rbS);
return rbS;
}
RadioButtonGroup<String> rbg = createYesNoField(this, "Output PDF?:","Y|N", false);
rbg.addSelectionListener(event -> {System.out.printf("%s | %s | %s | %s\n",
rbg.getValue(),
rbg.getSelectedItem(),
rbg.isEmpty(),
rbg.getOptionalValue()
);
});
rbg.setSelectedItem("g");
and the output is : g | Optional[g]
| false | Optional[g]