adding items into a selectOneMenu

HiAll,I am trying to create selectmenu dynamically at runtime. I am adding newly created components(like selectOneMenu..) to panelGrid so that I am able to see it on UI. I want to know I can add Items into selectMenu ? I want it for selectOneRadio also...Please help me....
[294 byte] By [phani_prakhyaa] at [2007-11-27 5:29:49]
# 1

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));

}

BalusCa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
sorry !!! Balusc... This is my piece of codeHtmlSelectOneMenu menu=new HtmlSelectOneMenu();menu.setId("menu1");panel.getChildren().add(menu); // Panel I binded to JSF page panelGrid...OUTPUT:combobox with zero elements.
phani_prakhyaa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Please tell me how cn I add Items programatically ...
phani_prakhyaa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Ah well, this way.List<SelectItem> selectItemList = populateSelectItemListSomehow();UISelectItems selectItems = new UISelectItems();selectItems.setValue(selectItemList);htmlSelectOneMenu.getChildren().add(selectItems);
BalusCa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

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

RahulSharnaa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
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 ;)
BalusCa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

> 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

RahulSharnaa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
It seems immutable indeed. Well after all this isn't exactly what the topicstarter was asking for.
BalusCa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
Thank you BalusC and Rahul, it is working ...Thank you for your help ... I know this word not sufficient...
phani_prakhyaa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

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 ?

phani_prakhyaa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
Please help me !!!!!!
phani_prakhyaa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
Please define "not working". Have you set a ValueExpression?
BalusCa at 2007-7-12 14:53:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...