aspose file tools*
The moose likes JSF and the fly likes populating SelectOneMenu using ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "populating SelectOneMenu using ArrayList" Watch "populating SelectOneMenu using ArrayList" New topic
Author

populating SelectOneMenu using ArrayList

Aparna Ram
Ranch Hand

Joined: Jan 27, 2006
Posts: 59
I have initiated an ArrayList which will hold the 50 US states in an Action class.
In the jsp page, the code goes like:


In the Action class,


I would like the dropdown menu to be populated with the states. Will this code work? Where am I going wrong, as it does not get populated?

PS: I am a total newbie to the field of programming. Kindly help. Thanks
chloe wong
Greenhorn

Joined: Aug 02, 2006
Posts: 17
public List getStateList()
{
List dropList= new ArrayList();
SelectItem selectItemForDropList = new SelectItem();
try
{
dropList.add(new SelectItem(String.valueOf("Alabama"),"Alabama"));
dropList.add(new SelectItem(String.valueOf("Alaska"),"Alaska"));
dropList.add(new SelectItem(String.valueOf("Arizona"),"Arizona"));

}
catch(Exception e)
{
//do something

}
return dropList;
}
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

When adding the element into the list, you should add the SelectItem object, not the String, e.g.
stateList.add(new SelectItem(1, "Alabama"));
stateList.add(new SelectItem(2, "Alaska"));
... etc


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
chloe wong
Greenhorn

Joined: Aug 02, 2006
Posts: 17
another way to do it will be..

private selectStateList = new ArrayList();

public List getStateList()
{
selectStateList .clear();
SelectItem selectItem = new SelectItem();
selectItem.setValue("Alabama");
selectItem.setLabel("Alabama");
selectStatusList.add(selectItem);

selectItem = new SelectItem();
selectItem.setValue("Alaska");
selectItem.setLabel("Alaska");
selectStateList .add(selectItem);
return selectStateList ;
}
Aparna Ram
Ranch Hand

Joined: Jan 27, 2006
Posts: 59
Chloe Wong, I tried using your code, but I receive the NullPointer Exception.. I dont understand why though
 
wood burning stoves
 
subject: populating SelectOneMenu using ArrayList