The moose likes JSF and the fly likes setting default value for a combobox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "setting default value for a combobox" Watch "setting default value for a combobox" New topic

setting default value for a combobox

ram an

Joined: Feb 12, 2009
Posts: 10
hi ,
i am new to JSF... the design is i have to add a combo box in the JSP page..the data for the combo box, i have called from the database.. when the page loads up i want the value in the combo box to set to default (eg: country = Tokyo)..but i cant set the value
to default...
the code is
<h:selectOneMenu value="#{requestscope.countryid}">
<f:selectItems value="#{pc_page1.countryid}" />

hw to set the default value whn the page loads up to the country i want in the combo box........the country list is in the database...
using JSF

please help
Kavita Tipnis
Ranch Hand

Joined: Sep 21, 2008
Posts: 177
after loading country list from the database just set countryid="Tokyo" in your backing bean
Rahul Santha

Joined: Apr 17, 2008
Posts: 12
<h:selectOneMenu id="corporationBI"

<f:selectItems value="#{pc_page1.countries}" />

Backing Bean
countries = new ArrayList<SelectItem>(10);
new SelectItem("NY", "NY")

When pc_page1.countryId = "NY", it gets selected in the UI.

Thanks!<br />Rahul, IOWA
ram an

Joined: Feb 12, 2009
Posts: 10
hey thanks fr all..i got it working...
Haritha Divi

Joined: Mar 04, 2009
Posts: 1
ram an wrote:hey thanks fr all..i got it working...

Hey, I've got a similar problem, can someone please help.
I need to create two combo boxes one for month and other for year.
The month cpmbo box (drop down) lists the 12 months but needs to display the current month as default and similarly year combo box lists from current year to 2004, defaulting to current year.
I've got two problems here:
1) to get default the month and year values displayed in the respective combo boxes
2) to get the drop down list starting from current year to 2004 displayed in the year combo box

I have coded the the list values as:
ArrayList Month = new ArrayList();
Month.add(new LabelValueBean("Jan", "01")); ---- the value '01' is written to DB
Month.add(new LabelValueBean("Feb", "02"));
Month.add(new LabelValueBean("Mar", "03"));
Month.add(new LabelValueBean("Apr", "04"));
Month.add(new LabelValueBean("May", "05"));
Month.add(new LabelValueBean("Dec", "12"));

I agree. Here's the link:
subject: setting default value for a combobox
It's not a secret anymore!