0

i want to add dynamically an new p:SelectOneMenu to my panelGrid here

<h:panelGrid id="panel_grid" columns="1" style="margin-bottom:10px"cellpadding="5" >

   <p:outputLabel for="firstQuestionDropDown" value={InsererAffaireController.firstQuestion.libeleQuestion}"
   <p:selectOneMenu id="firstQuestionDropDown" value="#{InsererAffaireController.firstQuestion.libeleQuestion}" style="width:150px">
      <p:ajax listener="#{InsererAffaireController.displayNextQuestion()}" update="panel_grid" />
      <f:selectItems value="#{InsererAffaireController.firstQuestion.listLibeleReponses}" />
    </p:selectOneMenu>

            <!-- Here i want to add my new SelectOneMenu -->    
</h:panelGrid>

I managed to do that from the managed bean but the problem is that i can't populate the new SelectOneMenu and i don't know how to add a f:selectItems to it here is the method that add the new p:SelectOneMenu to the panel grid but it still empty

    public void displayNextQuestion() {
       nextQuestion = getNextQuestion();
       OutputLabel nextQuestionLabelUI = new OutputLabel();
       nextQuestionLabelUI.setValue(nextQuestion.getLibeleQuestion());

       SelectOneMenu nextQuestionDropDown = new SelectOneMenu() ;

       List<SelectItem> selectItems = new ArrayList<>();

       for (String s : nextQuestion.getListLibeleReponses()) {

        selectItems.add(new SelectItem(s, s));
        System.out.println(selectItems.get(0).getLabel());
       }
       // How can i add the selectitems to the SelectOneMenu
       UIComponent panelGrid = findComponent("panel_grid");
       panelGrid.getChildren().add(nextQuestionLabelUI);
       panelGrid.getChildren().add(nextQuestionDropDown) ;
       RequestContext.getCurrentInstance().update("panel_grid");

}
LattaliAhmed
  • 134
  • 1
  • 2
  • 11
  • This all is the wrong approach. The correct approach is shown here: http://stackoverflow.com/q/3409053 – BalusC May 10 '16 at 12:11
  • i tried your approach and it's work for me but could you tell me why adding new UI components from the managed bean it's wrong ? – LattaliAhmed May 10 '16 at 22:03
  • 1
    Very verbose and cumbersome to maintain. XML+XHTML is a language much more suitable for declaring tree structures than Java. – BalusC May 11 '16 at 07:01

1 Answers1

1

You can use the UISelectItems class like this:

...
UISelectItems selectItemsComponent = new UISelectItems();
selectItemsComponent.setValue(selectItems);
nextQuestionDropDown.getChildren().add(selectItemsComponent);
...
JMSilla
  • 1,326
  • 10
  • 17