check the following example-
<h:selectOneMenu onchange="submit();"
binding="#{bean.inputCountry}"
immediate="false" style="width: 100">
<f:selectItems value="#{bean.countryItems}"/>
</h:selectOneMenu>
<h:outputText value="City:"/>
<h:selectOneMenu binding="#{bean.inputCity}"
immediate="false" style="width: 120">
<f:selectItems value="#{bean.cities}"/>
</h:selectOneMenu>
Bean code-
private HtmlSelectOneMenu inputCountry, inputCity;
private SelectItem[] countryItems = new SelectItem[]{
new SelectItem("India"),
new SelectItem("U.S."),
};
private SelectItem[] indiacityItems = new SelectItem[]{
new SelectItem("Mumbai"),
new SelectItem("Bangalore"),
new SelectItem("Delhi"),
new SelectItem("Chennai"),
};
private SelectItem[] uscityItems = new SelectItem[]{
new SelectItem("San Fransico"),
new SelectItem("New York"),
new SelectItem("Washington"),
};
public SelectItem[] getCities()
{
String countryKey="India";
if (inputCountry.getValue() != null)
countryKey = inputCountry.getValue().toString();
if(countryKey.equals("India"))
return indiacityItems;
else
return uscityItems;
}
public HtmlSelectOneMenu getInputCity() {
return inputCity;
}
public void setInputCity(HtmlSelectOneMenu inputCity) {
this.inputCity = inputCity;
}
public HtmlSelectOneMenu getInputCountry() {
return inputCountry;
}
public void setInputCountry(HtmlSelectOneMenu inputCountry) {
this.inputCountry = inputCountry;
}
public SelectItem[] getCountryItems() {
return countryItems;
}
public void setCountryItems(SelectItem[] countryItems) {
this.countryItems = countryItems;
}
Below is my code.i put it at prerender because i want item in first listbox based on previous page value.But i don't know how to loading items in the second listbox after a click which is fired by a command button.
public void prerender() {
try {
User[] userList = UserDAO.listUserByQuery(null,null);
Option[] optUser = new Option [userList.length];
int y = 0;
for (int i=0; i<userList.length; i++){
optUser [y] = new Option (String.valueOf(userList.getORMID()),userList.getFirstName());
if(userList.inCharge.contains(pInfo)){
this.listbox2.setItems(optUser);
}
else{
this.listbox1.setItems(optUser);
}
y++;
}
t.commit();>