Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

How to set default value into combobox - vaadin

FRANK NANDONG
5 years ago Aug 29, 2016 3:18am
Alejandro Duarte
5 years ago Aug 29, 2016 8:58am

Hi,

Can you describe what exactly you want to achieve? Looks like too much code to deal only with two genders ;) 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:

  public static class Person {
      ...
      private Gender gender;

      ... getters and setters ...
  }

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.

FRANK NANDONG
5 years ago Aug 29, 2016 9:23am
FRANK NANDONG
5 years ago Sep 01, 2016 4:03am

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<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;
                     }
                     }

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.