Hi guys, I am new to the forums and also still quite new to Vaadin in general.
I have a question regarding optionGroups
I have multiple optionGroups due to some restrictions in my Uni project, so I have a for loop for example like following example:
What I want to do is: Since I have multiple optionGroups ( I cannot change that, due to restrictions from other Teammembers and so on) I want to clear all optionGroups (except for the selected one) every time the value changes.
But this does not seem to be working. optionGroup.clear(); somehow changes the value of selection to null. So this triggers another ValueChangeListener??
I simply want to loop over the array of OptionGroups I have (could be multiple or just one, thats dynamic) and then deselect all optionGroups once a specifc one was selected. Basically like radiobuttons within an OptionGroup. Same logic but for multiple OptionGroups that each have only one element
OptionGroup 1
OptionGroup 2 X
OptionGroup 3
Hope this is somewhat clear. Thanks for the help!
ArrayList<OptionGroup> optionGroups = new ArrayList<OptionGroup>();
for (CarModel carModel : cars.getModels()) {
OptionGroup selection = new OptionGroup();
selection.addItem(cars.getModel(carModel.getName()));
selection.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
for(OptionGroup optionGroup : optionGroups){
if(optionGroup != selection){
optionGroup.clear();
}
}
}
}
}
if (event.getProperty().getValue() != null) {
for (...) {
...
}
}
That way you’ll only do the mass-clearing when the new value is an actual value, and do nothing when the value is cleared.
Thank you for your suggestion. Ok I will try this out and hope it gets the right results.
However I do have one more question: Technically, my approach should be correct right?
–>I select Radio Button A —> Radio Buttons B & C (for example) are cleared —> I select Radio Button B now —> A & C get cleared and so on … or am I doing a fundamental mistake in my approach?
Ok so I tried using the unselect option provided by optionGroup but I have no idea how it is intended to work.
First I retrieve a collection of all ItemIDs then I turn this into a string and tell optionGroup to unselect this radiobutton ( since every optionGroup only has 1 item in it, this should work as intended no?)
for (Object itemId : items) {
optionGroup.unselect(itemId);
}
or
optionGroup.unselect(items.get(0));
because it’s still a Collection instead of a single item, even if it only contains one, but if I recall correctly it should work just as well with
I figured out how to unselect without actually setting anything to null. I used the unselect statement after using the iterator on the collection I received. This seems to be working just fine now! But thanks a lot for your help & effort
if (optionGroup != selection){
Collection items = optionGroup.getItemIds();
optionGroup.unselect(items.iterator().next());
}