Search code examples
jsf-2primefaces

Create SelectOneMenu UIComponent from Server side


I have created a SelectOneMenu uicomponent

SelectOneMenu value = new SelectOneMenu();

I want to insert some selectItems in the SelectOneMenu. I tried this

String[] options = question.getOptions().split(",");
for(String option : options){
    SelectItem selectItem = new SelectItem();
    selectItem.setLabel(option);
    selectItem.setValue(option);
    value.getChildren().add(selectItem);
}

But when i add the selectItem i am getting error that add(uicomponent) is not applicable for arguments SelectItem. What to do, any suggestions?


Solution

  • Well it's failing because javax.faces.model.SelectItem is not a UIComponent. What you should have is the UISelectItem. So your code should look more like

        String[] options = question.getOptions().split(",");
    for(String option : options){
        UISelectItem selectItem = new UISelectItem();
        selectItem.setItemLabel(option);
        selectItem.setItemValue(option);
        value.getChildren().add(selectItem);
    }