Deselect individual items in one or more OptionGroups

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();

            } 
          }
       }
    }
}

You could add before the for loop another check that the new value that caused the event isn’t null?

What exactly do you mean by that? So once I clear the optionGroup, it will be null right? So what will I check before the loop?

something along the lines of

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?)

if(optionGroup optionGroup : optionGroups){
Collection items = optionGroup.getItemIds();
optionGroup.unselect(items.toString());
}

I’m really confused with this. How do I get it to work properly?

No, if you use unselect you’ll need to use e.g.

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

optionGroup.setValue(null);

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 :slight_smile:

if (optionGroup != selection){ 
   Collection items = optionGroup.getItemIds();
   optionGroup.unselect(items.iterator().next());
}