9 Replies Latest reply on Jun 1, 2009 4:33 AM by dan.j.allen

    rich : pickList

      Hi.

      I have a rich:pickList component and I select some items from the left list into the right list. How can have access to the elements I selected and are now in the right list? They are a list of SelectItem objects, each SelectItem object composed of a Participant Object and some String for label (as seen in the 2 pieces of code below). I want to be able to have access at the participant object, so, i can get the information from that object.

      Thx.

      The JSF code:

      <rich:pickList id="pickList">
       <f:selectItems value="#{registerParticipant.participantsSelectItem}"/>
      </rich:pickList>


      The Java code:

      private ArrayList participantsSelectItem;
      
      public void setParticipantsSelectItem(ArrayList<String> participants) {
       this.participantsSelectItem = participants;
       }
      
      public List getParticipantsSelectItem() {
       participantsSelectItem = new ArrayList();
       for (int i=0; i<Authenticator.getParticipants().size();i++)
       {
       String fullName = Authenticator.getParticipants().get(i).getFirstName()+" "+Authenticator.getParticipants().get(i).getLastName();
       participantsSelectItem.add(new SelectItem(Authenticator.getParticipants().get(i), fullName));
       }
      
       return participantsSelectItem;
      }


        • 1. Re: rich : pickList
          ilya_shaikovsky

          check livedemo sample. There are last sample which manages selected objects via ajax.

          • 2. Re: rich : pickList
            dan.j.allen

            I believe that you treat the rich:pickList just like you would an h:selectManyListbox. It's just a prettier, more user-friendly UI widget. So the value of the component is a list of items that accept the type of each item value.

            <h:pickList value="#{bean.selectedItems}">
             <f:selectItems value="#{bean.options}"/>
            </h:pickList>


            The property selectedItems would be defined as follows:

            private List<String> selectedItems;
            
            public List<String> getSelectedItems() { return selectedItems; }
            public setSelectedItems(List<String> items) { this.selectedItems = items; }


            And of course the options would be a property which provides a list of SelectItem objects as you have already done.

            • 3. Re: rich : pickList

              The thing is that when I select the items from the list into the right list the participantsSelectItem ArrayList has the initial length with all the objects (not just the ones I copied in the right list - that's the thing I need, an ArrayList with just the objects in the right list).


              private ArrayList<SelectItem> participantsSelectItem = new ArrayList<SelectItem>();
              
              public void setParticipantsSelectItem(ArrayList<SelectItem> participants) {
               this.participantsSelectItem = participants;
               }
              
               public List getParticipantsSelectItem() {
               participantsSelectItem = new ArrayList();
               for (int i=0; i<Authenticator.getParticipants().size();i++)
               {
               String fullName = Authenticator.getParticipants().get(i).getFirstName()+" "+Authenticator.getParticipants().get(i).getLastName();
               participantsSelectItem.add(new SelectItem(Authenticator.getParticipants().get(i), fullName));
               }
              
               return participantsSelectItem;
               }


              • 4. Re: rich : pickList

                Ok, I succedeed in obtaining the list of object in the right list. The issue is that I obtain an Array$ArrayList of String objects even though in the code below I put in the SelectedItem constructor a Participant Object.


                public List getParticipantsSelectItem() {
                 participantsSelectItem = new ArrayList();
                 for (int i=0; i<Authenticator.getParticipants().size();i++)
                 {
                 String fullName = Authenticator.getParticipants().get(i).getFirstName()+" "+Authenticator.getParticipants().get(i).getLastName();
                 participantsSelectItem.add(new SelectItem(Authenticator.getParticipants().get(i), fullName));
                 }
                
                 return participantsSelectItem;
                 }



                And the participants list to where I return the values from the right list: value="#{registerEvent.participants}"
                <rich:pickList id="pickList" rendered="true" value="#{registerEvent.participants}">
                 <f:selectItems value="#{registerEvent.participantsSelectItem}"/>
                </rich:pickList>


                My participants list looks like this from debugger:
                participants=Arrays$ArrayList
                 a=String[3] //there are 3 elements copied in the right list
                 [0]=Participants@1a59ac1
                 [1]=Participants@...
                 [2]=Participants@....


                • 5. Re: rich : pickList

                  Is there a way to obtain the ArrayList with the Participants objects instead of the list with String objects ?

                  • 6. Re: rich : pickList
                    dan.j.allen

                    You do have a List, it's just a non-modifiable ArrayList created by Arrays.toList. That is just the way the rich:pickList creates the collection of selected items. In your component, you will have to copy it to create a modifiable ArrayList.

                    List participantsCopy = new ArrayList(participates);

                    • 7. Re: rich : pickList

                      Ok, I make that list, but the problem remains the same. The new list has the objects i selected but are not the participants objects, are the same string objects. Each index of the list has an element like this: "Participants@2f6c.." and it's something like the signature of the object - a string a value and not the Participant object itself!!

                      participantsCopy = ArrayList<E>
                       elementData=Object[3]
                       [0] = "Participants@3f5f1.."
                       [1] = "Participants@.."
                       //.......


                      I have even parametrize the ArrayCollection with but participantsCopy still remains with parameter when i debug.

                      • 8. Re: rich : pickList

                        I have even parametrize the ArrayCollection with

                        <Participants>
                        but participantsCopy still remains with
                        <E>
                        parameter when i debug.

                        • 9. Re: rich : pickList
                          dan.j.allen

                          Stop using ArrayList except for where you construct it. That is where things are getting confused. ArrayList is an implementation of List, not an interface. Therefore, when you have that signature in your method you confuse RichFaces (I have no doubt).