5 Replies Latest reply on May 18, 2011 11:45 PM by adballeras

    SelectOneMenu output display problem

    abdoulmou3in

      Hi all,

       

      I'm facing a problem with selectOneMenu when populating it with a list.

       

      in my xhtml page i have the following code

       

       

      <h:selectOneMenu > 
      <f:selectItems value="#{validateEntryPersonne.civilites}" itemLabel="#{validateEntryPersonne.civilites}"/> 
      </h:selectOneMenu>
      

      My bean is as follows

       

      public List<TCivilite> civilites = null; 
                public List<TCivilite> getCivilites() {
                          Statement stmt = null;
                          civilites = new ArrayList<TCivilite>();
      
        civilites.add(new TCivilite("VAR1", "VAR2", "VAR3", "VAR4", "VAR5"));
        civilites.add(new TCivilite("VAR1", "VAR2", "VAR3", "VAR4", "VAR5"));
        civilites.add(new TCivilite("VAR1", "VAR2", "VAR3", "VAR4", "VAR5"));
        civilites.add(new TCivilite("VAR1", "VAR2", "VAR3", "VAR4", "VAR5"));
      
                          return civilites;
                }
      
      

       

       

      When i execute this code the selection list shows entries looking like this : MyPackage@AlphaNumeric (Example : com.package.jsf@1ee6ed5 )

       

      when i change list declaration from public List<TCivilite> civilites = null; to private List legumes = null; it works fine.

       

      How can i user thetype <TCivilite> in order to display it correctly.

       

      Thanks in advance

       

      Best Regards

        • 1. SelectOneMenu output display problem
          iabughosh

          welcome to the forum abdoulmou3in, what is the version of JSF, RF ?

          • 2. Re: SelectOneMenu output display problem
            h2g2

            Hi,

             

            at first, I think a f:selectItems objet can only be mapped to an array of SelectItems (such as :

            ArrayList<SelectItem> civilites= new ArrayList<SelectItem>();

             

             

            And since it's a list of both "value" and "label" items, you needn't a "itemLabel" in your f:selectItems. That's why this attribute does not exist for the tag f:selectItems (or it would be called instead "itemLabels" or something like that).

             

            Eventually, you need to populate such a list with a list of SelectItems<Value,Label> where the value will be the key to know which item is selected (or to set it from your bean) , and the label the displayed entry for the user.

            For those, you can for exemple put some string holding the "VAR" in your TCivilites, such as

             

             

            for (TCivilite civilite : AllCivilites){
                 civilites.add(newSelectItems(civilite.id,Integer.toString(civilite.var1)+Integer.toString(civilite.var2)));
                 }

             

             

             

             

            Hope it will help.

            • 3. SelectOneMenu output display problem
              abdoulmou3in

              Hi all,

               

              Thak you very much for your unswers.

              For now i'm wrinting from train using m'y phone.

               

              Ibrahim, i'm using eclips 3.6 Helios / tomcat 7 / JSF 2 majora / RICHFACES 4 and hibernate 2.3

               

              H2g2, i'll try to mofdify m'y code and let you know.

               

              Thanks again for all of you.

              • 4. SelectOneMenu output display problem
                iabughosh

                there is a new feature in JSF 2 allows you to do that :

                <h:selectOneMenu>

                                                        <f:selectItems value="#{mybean.list}"

                                                                                         var="item"

                                                                                         itemLabel="#{item.name}"

                                                                                         itemValue="#{item.value}"/>

                                              </h:selectOneMenu>

                where mybean.list is an ArrayList<MyBean>.

                 

                regards.

                • 5. Re: SelectOneMenu output display problem
                  adballeras

                  Make sure that value for the itemLabel is of type String or it will use toString method of that object. In your case it is getting the return value of toString method in TCivilite.

                   

                  If you have for example a property name of type String in TCivilite:

                   

                  <h:selectOneMenu value="#{yourBean.selected}">
                       <f:selectItems value="#{yourBean.list}"
                                             var="item"
                                             itemLabel="#{item.name}"
                                             itemValue="#{item}" />
                  </h:selectOneMenu>
                   
                  
                  

                   

                  You also need to have a property of type TCivilite with matching getter and setter methods in order to get what is selected by the user.