Option Group in Form Issue solved

Hi…Vaadins,

I think it’s really need to worry while working with
Radio Buttons
in the
Vaadin Form
. I am trying to achieve Radio Buttons (with caption “Hide” and "Visible) in the Form. UI is OK. Radio Buttons are looking really very charming. :slight_smile: But the problem is
No any button is Preselected
. How one could see the item selected by default.
Form has two
editboxes
also. And working fine. The only issue is with Radio Buttons

And the most weird thing is "
optionGroup.isSelected
" is returning
“true”
. I have made all the possible effort from my side. I wasted 2 days on it with no luck till now. really frustrated…!!

Any help is appreciated…

Thanks in advance…
Warm regards.!!

Here is the code -

public class ItemUpdate extends Window implements Button.ClickListener,FormFieldFactory
{
/**
*
*/
private static final long serialVersionUID = 1L;
private final Item item;
private Form editorForm;
private Button saveButton;
private Button cancelButton;
ItemContainer imc=new ItemContainer();

public ItemUpdate(Item item)
{
	
	this.item=item;
	
	editorForm=new Form();
	editorForm.setFormFieldFactory(this);
	editorForm.setItemDataSource(item,Arrays.asList("itemQuantity","timeToShip","visibilityStatus"));
	
	editorForm.setWriteThrough(true);
	editorForm.setImmediate(true);

	saveButton = new Button("Save", this);
	cancelButton = new Button("Cancel", this);	
	editorForm.getFooter().addComponent(saveButton);
        editorForm.getFooter().addComponent(cancelButton);
        getContent().setSizeUndefined();
	addComponent(editorForm);
	setCaption("Update Item");
	
	center();

}

@Override
public Field createField(Item item, Object propertyId, Component uiContext) {
	
	if("visibilityStatus".equals(propertyId))
	{
                    // final List<String> options = Arrays.asList(new String[] { "Hide", "Visible"});
                    // OptionGroup optionGroup=new OptionGroup(" ", options);

		OptionGroup optionGroup=new OptionGroup("VisibilityStatus");
		optionGroup.addItem("Hide");
		optionGroup.addItem("Visible");
                    optionGroup.select("Hide");
                    optionGroup.setImmediate(true);
		return optionGroup;

		
	}
		Field field = DefaultFieldFactory.get().createField(item, propertyId,uiContext);
		
	    return field;

}

}

I have used
Indexted Container
also but failed.

Here is the code snippet -

            IndexedContainer indxContnr=new IndexedContainer();
	//indxContnr.addContainerProperty("Visibility Status",String.class,null);
	indxContnr.addItem("Hide");
	indxContnr.addItem("Visible");
	//optionGroup.setItemCaptionPropertyId("Visibility Status");
	optionGroup.setContainerDataSource(indxContnr);
	optionGroup.setImmediate(true);
	optionGroup.setNullSelectionAllowed(false);
	System.out.println(indxContnr.firstItemId());
	optionGroup.setValue(indxContnr.firstItemId());
	optionGroup.select(indxContnr.firstItemId());
	optionGroup.setImmediate(true);

	//System.out.println(og.isSelected("Hide"));

Hi…all,

After banging my head with Form and OptionGroup, I got the solution… :slight_smile: There may be some better solution but the trick which worked for me is - adding custom fields into the Form. Avoid using setItemDataSource() and create your own fields with addField().
Here is the code -

            itemCode.setValue(item.getItemProperty("itemCode").toString()); //Create text field.
	itemQty.setValue(item.getItemProperty("itemQuantity").toString()); //Create text field.
	
	String str = item.getItemProperty("visibilityStatus").toString();
	if (str.equals("0"))
	{
		group.select("Visible");
	}
	else
	{
		group.select("Hide");
	}
	
	
	editorForm=new Form();
	editorForm.addField("Item Code", itemCode);
	editorForm.addField("Item Quantity", itemQty);
	editorForm.addField("Visibility Status", group); //Create option group.

Thank you very much , it worked !!!