Radio Button Group setSelectedItem not working?

Hi I’m using Vaadin 8.0.4

I’m having no luck with preselecting a RadioButtonGroup option.

Is this a known issue or is there another method for this?

Thanks!

Hi,

can you share some code of how you’re populating the component and selecting the value?

-Olli

Hi Olli,

Thanks for replying, my apologies for the delayed response, was shifting offices.

Below is how I’m initializing the RadioButtonGroup. Gender.find returns an ArrayList

RadioButtonGroup<Gender> mGenderSel;
[/code][code]
mGenderSel = new com.vaadin.ui.RadioButtonGroup("Gender", Gender.find(null, null));
mGenderSel.setItemCaptionGenerator(Gender::getName);

The above code correctly shows the items of ArrayList.

This is how I’m trying to set value;

mGenderSel.setSelectedItem(gender); On debugging object ‘gender’ is initialized, but it’ not showing up as selected in the browser, at another place in another RadioButtonGroup I’m using a String object and it’s working there as expected, could it be it’s broken for custom objects?

Thanks,
Sid

Hi, it doesn’t implement an equals, I’m using similar objects in ComboBoxes and they work fine there for settting values.
The items are database driven so the items are identical but the assigned value is a new instance of the object that is present in the list provided to setItems().

Hi,

does your Gender class implement .equals()? I’m wondering if the instance returned by Gender.find() is a different from the one you pass to setSelectedItem()

-Olli

Yeah, you’ll need to implement equals() in your Gender class. I tested it out with the following:

  public class Gender {
    private String name;

    public Gender(String name) {
      this.name = name;
    }

    public String getName() {
      return name;
    }

    // comment this method out to see that setSelectedItem doesn't work without it
    @Override
    public boolean equals(Object obj) {
      if ((obj == null) || !(obj instanceof Gender)) {
        return false;
      }
      return ((Gender) obj).getName().equals(this.getName());
    }
  }
  
    @Override
  protected void init(VaadinRequest vaadinRequest) {
    final VerticalLayout layout = new VerticalLayout();
    RadioButtonGroup<Gender> mGenderSel;
    List<Gender> beans = new ArrayList<>();
    Gender gender1 = new Gender("foo");
    Gender gender2 = new Gender("bar");
    Gender gender3 = new Gender("baz");
    beans.add(gender1);
    beans.add(gender2);
    beans.add(gender3);
    mGenderSel = new com.vaadin.ui.RadioButtonGroup("Gender", beans);
    mGenderSel.setItemCaptionGenerator(Gender::getName);
    Gender gender4 = new Gender("baz"); // same as gender3? Only if Gender implements equals() 
    mGenderSel.setSelectedItem(gender4);
    layout.addComponent(mGenderSel);

    setContent(layout);
  }

-Olli

Hi Olli, thank you for writing all that down )

I implemented the equals function. Noticed it runs only if the RadioButtonGroup was previously set, even when it runs it doesn’t visibly change the radio button selection. So I tried to check if the value was set and indeed the genSel.getValue() was correctly returning the Gender object.

Gender gender = user.getGender(); if ( gender.equals(mGenderSel.getValue()) ) { Notification.show("genders are set and equal"); } mGenderSel.setValue(gender); mGenderSel.setSelectedItem(gender); if ( gender.equals(mGenderSel.getValue()) ) { Notification.show("genders are set and equal"); } Something odd going on here? Checked in the debugger too, the correct value is returned.

It’s just not reflecting the correct selection visually.

Strange, it works for me. Have you tried with 8.0.5? That’s what I’m using.

-Olli

Same issue as Siddharth Mody. Once the RadioButtonGroup is displayed, “setSelectedItem” makes no difference (at least visually, internally the selected item is correct). I am using Vaadin 8.1.7.

I have the same problem as stated above. Visually, this doesn’t reflect. Any ideas ?