jsf selectonemenu dynamically setvalue

798 views Asked by At

I am trying to set the value of a SelectOneMenu programmatically but the value doesn't get set.

Tried searching the solution a lot but couldn't find a satisfactory answer:

Here is the code. To Create the SelectOneMenu:

SelectOneMenu yesNoDropdown = new SelectOneMenu();
List<SelectItem> items = new ArrayList<SelectItem>();
items.add(new SelectItem("", ""));
items.add(new SelectItem("yes", "Yes"));
items.add(new SelectItem("no", "No"));

UISelectItems selectOptions = new UISelectItems();
selectOptions.setValue(items);

yesNoDropdown.getChildren().add(selectOptions);
yesNoDropdown.setId("yes-no");
yesNoDropdown.setRequired(true);

And I am trying to set the value using this code:

FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot rootView = context.getViewRoot();
SelectOneMenu yesNoDropdown = (SelectOneMenu) rootView.findComponent("formId:yes-no");
yesNoDropdown.setValue("no");
RequestContext.getCurrentInstance().update("formId");

Not sure what is wrong. Any help will be appreciated.

1

There are 1 answers

0
GSS On

I solved it just now : did this :

yesNoDropdown.setSubmittedValue("no");

Thanks!