Combo Box Problem

Hear show my combo box and how i initialize value when form load.


 cmbTitle = new ComboBox();
        cmbTitle.setImmediate(false);
        cmbTitle.setHeight("30px");
        cmbTitle.setWidth("60px");
        cmbTitle.setCaption("Title");

        cmbTitle.setInputPrompt("select sex");
       sex = new String[]{"G", "F"};
        for (int j = 0; j < sex.length; j++) {
            cmbTitle.addItem(sex[j]
);
        }
        cmbTitle.addItem(na);
        mainLayout.addComponent(cmbTitle, "top:130.0px;right:506.0px;bottom:654.0px;left:220.0px;");

In my form contain button, if i press button combo box value must visible to “M” (default value is “select sex”).

Below code show how I try it.

cmbTitle.addItem(sexTyp);
cmbTitle.setNullSelectionItemId(sexTyp);

but still it not working, please help me

Hi,check
Vaadin Translation
we did the same type of thing with internal and display values and defaulting a value.
Regards P

Thanks for reply. I did it below show my code.


private final String[] sex = new String[]
{"Male", "Female"};

cmbSex = new ComboBox();
        cmbSex.setImmediate(false);
        cmbSex.setHeight("60px");
        cmbSex.setWidth("50px");
        cmbSex.setCaption("Sex");
        cmbSex.setNullSelectionAllowed(false);
        cmbSex.setInputPrompt("Sex");
        for (String s : sex) {
            cmbSex.addItem(s);
        }
        mainLayout.addComponent(cmbSex,
                "top:290.0px;right:468.0px;bottom:588.0px;left:520.0px;");

Above code show how I display my combo box .

After press button combo box value change, below show that code


cmbSex.setNullSelectionAllowed(false);
        cmbSex.setValue("M");
        cmbSex.setImmediate(true);

Hi,

I’m not sure I understand the whole problem, but if you just want to have a different value and caption for each item, you should add properties to the ComboBox:

ComboBox cmbTitle = new ComboBox();
cmbTitle.setImmediate(false);
cmbTitle.setHeight("30px");
cmbTitle.setWidth("60px");
cmbTitle.setCaption("Title");
cmbTitle.setInputPrompt("select sex");
[b]
cmbTitle.addContainerProperty("caption", String.class, "");
[/b] // add property
[b]
cmbTitle.setItemCaptionPropertyId("caption");
[/b] // use property for caption
String[] sex = new String[]
 { "G", "F" };
[b]
String[] caps = new String[]
 { "Guy", "Fox" };
[/b] // You might want to adjust these 
for (int j = 0; j < sex.length; j++) {
        [b]
Item item = cmbTitle.addItem(sex[j]
);
[/b]
        [b]
item.getItemProperty("caption").setValue(caps[j]
); // set caption property
[/b]
}

Best Regards,
Marc

Update: Uh, and you probably want to add cmbTitle.setNullSelectionAllowed(false);