Just add it to the array or List behind the valuebinding of f:selectItems.
Basic example:
<h:selectOneMenu value="#{myBean.selectedItem}">
<f:selectItems value="#{myBean.selectItems}" />
</h:selectOneMenu>
<h:inputText value="#{myBean.newItem}" />
<h:commandButton value="#{myBean.addNewItem}" />
MyBeanpublic void addNewItem() {
selectItems.add(new SelectItem(newItem));
}
Alright here is an Example for
<h:selectOneMenu value="#{sampleBean.selectedItem}">
<f:selectItems value="#{sampleBean.itemsList}" />
</h:selectOneMenu>
import java.util.List;
import java.util.ArrayList;
import javax.faces.model.SelectItem;
public class SampleBean{
private List<SelectItem> itemsList;
private String selectedItem;
public SampleBean(){
this.itemsList = new ArrayList<SelectItem>();
SelectItem si = new SelectItem("value1","label1");
this.itemsList.add(si);
si = new SelectItem("value2","label2");
this.itemsList.add(si);
si = new SelectItem("value3","label3");
this.itemsList.add(si);
si = new SelectItem("value4","label4");
this.itemsList.add(si);
}
public void setItemList(List<SelectItem> itemsList){
this.itemsList = itemsList;
}
public List<SelectItem> getItemList(){
return this.itemsList;
}
public void setSelectedItem(String selectedItem){
this.selectedItem = selectedItem;
}
public String getSelectedItem(){
return this.selectedItem;
}
}
Hope this might help :)
REGARDS,
RaHuL
> OK?
>
> Rahul, this is not going to work, this will produce a
> list of 4 same selectitem references with value4 and
> label4, because you're overwriting the 'si' reference
> everytime which is going to be reflected in the
> collection. Test Code before Posting Code ;)
I did...It works well
my friend guess you need to get a clearity here
I'm adding 4 newly created Objects references to the List.
I'd be glad if you can try that yourself and get back :)
Hope there are no hard issues...
REGARDS,
RaHuL
Here another problem ..
I am able add Items to SelectOneMenu and selectOneRadio but
I am not able to set any Item thorugh programatically..
I tried these options
HtmlSelectOneRadio radio=new HtmlSelectOneRadio();
radio.setId("radio_tab2_line1_"+serviceNumber);
list=new ArrayList();
item=new SelectItem("Yes","Yes");
list.add(item );
item=new SelectItem("No","No");
list.add(item );
items= new UISelectItems();
items.setValue(list );
radio.getChildren().add( items);
radio.setValue(radio.getChildren().get(0) );
// and radio.setValue("Yes"); also
Nut not workign ..can u help me ?