I hope we all know how to create selectOneChoice using Model layer and previously I have posted about populating selectOneChoice programmatically from bean using ArrayList
Programmatically populate values in a af:selectOneChoice component in ADF
But this post is slight different - It is about creating selectOneChoice component and applying selectItems to it programmatically
There is nothing complicated in this , Just a piece of code is required
Here I have used an ArrayList to set SelectItems and created component binding for af:form to add selectOneChoice as it's child
Packages used in bean-
Dropped a button on page and created an ActionListener in managed bean , In this method I am creating selectOneChoice component and adding selectItems to it
Now run and check application- On click of button , SelectOneChoice is created :)
Sample ADF Application (Jdev 12.1.3) - Download
Cheers :) Happy Learning
Programmatically populate values in a af:selectOneChoice component in ADF
But this post is slight different - It is about creating selectOneChoice component and applying selectItems to it programmatically
There is nothing complicated in this , Just a piece of code is required
Here I have used an ArrayList to set SelectItems and created component binding for af:form to add selectOneChoice as it's child
Packages used in bean-
importjava.util.ArrayList;
importjava.util.List;
importjavax.faces.component.UISelectItems;
importjavax.faces.event.ActionEvent;
importjavax.faces.model.SelectItem;
importoracle.adf.view.rich.component.rich.RichForm;
importoracle.adf.view.rich.component.rich.input.RichSelectOneChoice;
//af:form binding to add selectOneChoice as it's child
private RichForm rootFormBind;
publicvoidsetRootFormBind(RichForm rootFormBind){
this.rootFormBind= rootFormBind;
}
public RichForm getRootFormBind(){
return rootFormBind;
}
//List variable to populate selectOneCoice
List<SelectItem> customList;
publicvoidsetCustomList(List<SelectItem> customList){
this.customList= customList;
}
public List<SelectItem>getCustomList(){
if(customList ==null){
customList =new ArrayList<SelectItem>();
customList.add(new SelectItem("i1","Item 1"));
customList.add(new SelectItem("i2","Item 2"));
customList.add(new SelectItem("i3","Item 3"));
customList.add(new SelectItem("i4","Item 4"));
customList.add(new SelectItem("i5","Item 5"));
}
return customList;
}
Dropped a button on page and created an ActionListener in managed bean , In this method I am creating selectOneChoice component and adding selectItems to it
/**Method to create SelectOneChoice programmatically and add Items to it
* @param actionEvent
*/
publicvoidcreateSOCAction(ActionEvent actionEvent){
//Create SelectOneChoice component using It's Java Class
RichSelectOneChoice selectOneChoice =new RichSelectOneChoice();
//Set required proprties of component
selectOneChoice.setId("soc1");
selectOneChoice.setLabel("Choice List");
//Create SelectItems component
UISelectItems selectItems =new UISelectItems();
//Set Id
selectItems.setId("si1");
//Set SelectItems value, It'll be derived from an ArrayList of type javax.faces.model.SelectItem
selectItems.setValue(getCustomList());
//Add selectItems component as child of selectOneChoice
selectOneChoice.getChildren().add(selectItems);
//Finally add SelectOneChoice to any root component of page
getRootFormBind().getChildren().add(selectOneChoice);
AdfFacesContext.getCurrentInstance().addPartialTarget(rootFormBind);
}
Now run and check application- On click of button , SelectOneChoice is created :)
Sample ADF Application (Jdev 12.1.3) - Download
Cheers :) Happy Learning