background image

Properties for SelectItems

<< UISelectItem Properties | Initializing optionsGroup >>
<< UISelectItem Properties | Initializing optionsGroup >>

Properties for SelectItems

Properties for SelectItems Composed of SelectItem Instances
"Using the selectItems Tag" on page 347
describes how the newsletters list of the Duke's
Bookstore application is populated using the application configuration resource file. You can
also populate the SelectItems with SelectItem instances programmatically in the backing
bean. This section explains how to do this.
In your backing bean, you create a list that is bound to the SelectItem component. Then you
define a set of SelectItem objects, set their values, and populate the list with the SelectItem
objects. Here is an example code snippet that shows how to create a SelectItems property:
import javax.faces.component.SelectItem;
...
protected ArrayList options = null;
protected SelectItem newsletter0 =
new SelectItem(
"200", "Duke's Quarterly", "");
...
//in constructor, populate the list
options.add(newsletter0);
options.add(newsletter1);
options.add(newsletter2);
...
public SelectItem getNewsletter0(){
return newsletter0;
}
void setNewsletter0(SelectItem firstNL) {
newsletter0 = firstNL;
}
// Other SelectItem properties
public Collection[] getOptions(){
return options;
}
public void setOptions(Collection[] options){
this.options = new ArrayList(options);
}
The code first initializes options as a list. Each newsletter property is defined with values. Then,
each newsletter SelectItem is added to the list. Finally, the code includes the obligatory
setOptions
and getOptions accessor methods.
Properties for SelectItems Composed of SelectItemGroup Instances
The preceding section explains how to write the bean property for a SelectItems component
composed of SelectItem instances. This section explains how to change the example property
from the preceding section so that the SelectItems is composed of SelectItemGroup
instances.
Writing Bean Properties
The Java EE 5 Tutorial · September 2007
386