How to set default value into combobox - vaadin

Good Day Expert!

How to set default value into combobox :

Here is my source codes, i would like make the values that passing from method setGender():

// generate Genders into combobox value
        Collection<GenderTable> gendertable= getRecordGenders();
        Container container= new ListContainer<RefGenders>(GenderTable.class, genderTable);
        combobox_gender.setContainerDataSource(container);
        combobox_gender.setItemCaptionPropertyId("nameGender");


noid_textfield.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
String gender = setGenders();
String genderName = "";

if("F".equalsIgnoreCase(gender)){
                    genderName ="Female";
                }else{
                    genderName ="Male";
                }

GenderTable genderTable = new RefGenders(gender, genderName);
PropertysetItem item = new ProppertysetItme();
FieldGroup binder = new FieldGroup(item);
binder(combobox_gender , "GenderTable");
    }
        });

The result at display page , the Combobox Gender - “combobox_gender” are gray. and its not bind properly. any solution that can fix this.


Thank you Very much!

Hi,

Can you describe what exactly you want to achieve? Looks like too much code to deal only with two genders :wink: In any case, I’ll share some ideas that might help. I would define an enum like the following:


public enum Gender { FEMALE, MALE }

And then just add the options to the ComboBox like this:


ComboBox gender = new ComboBox();
gender.addItems(Gender.values());

And if you want to set a “default value” you can do:


gender.setValue(Gender.FEMALE);

Also, if you have a domain class like this:

[font=courier new]
public static class Person {

private Gender gender;

  ... getters and setters ...

}
[/font]

You can bind to a particular instance using the
BeanFieldGroup.bindFieldsUnbuffered
static method.

But I don’t understand well your use case particularly the TextField and its change listener.

Hi Alejandro,

I did that because my country using ID card number that contain number of male or female in noid_textfield. So that’s why i used TextField with change Listener. From the number of TextField it wll load combobox Gender with a default value that set from listener textfied ID without removing the gender name list.

  • Above codes its just example that will help me as a based.

  • I would like result like below. which default value is male which get from last number from textfield, if even is for Male, uneven is for Female:

  • If you can give me how to set default value to Combobox using FieldGroup bind, its really awesome to me.

Good Day!

I have found the solutions for this , here is my code:

// generate Genders into combobox value
        Collection<GenderTable> gendertable= getRecordGenders();
        Container container= new ListContainer<RefGenders>(GenderTable.class, genderTable);
        combobox_gender.setContainerDataSource(container);
        combobox_gender.setItemCaptionPropertyId("nameGender");

noid_textfield.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
// getGenders(); get the return gender code


               @SuppressWarnings("unchecked")
                Iterator<GenderTable> iterator = (Iterator<GenderTable>)  combobox_gender.getItemIds().iterator();
              
              while(iterator.hasNext()) {
                     GenderTable obj = iterator.next();
                     if(obj.getcodeGender().equals(getGender().trim())){
                         gender_combo.setValue(obj);
                         break;
                     }
                     }
    }
        });

I Have changed with this code:
@SuppressWarnings(“unchecked”)
Iterator iterator = (Iterator) combobox_gender.getItemIds().iterator();

          while(iterator.hasNext()) {
                 GenderTable obj = iterator.next();
                 if(obj.getcodeGender().equals(getGender().trim())){
                     gender_combo.setValue(obj);
                     break;
                 }
                 }

a) I using Iterator the combobox by defining into Bean GenderTable.
b) I compare the code Gender from value in combobox_gender with method getGender() which will returning of code gender.