Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JSF: Dynamically building SelectItems

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default JSF: Dynamically building SelectItems

    Hi Guys and gals,

    I have been banging my head against this wall for a couple days and am coming up to the stage where I just put in an ugly work around, but I would rather solve it and learn something, so any help would be very much appreciated.

    I need to dynamically populate a h:selectManyListbox, I am using the f:selectItems linked to an array list of selectItem 's on my backing bean, as follows:

    <h:selectManyListbox styleClass="selectManyListbox" id="avaliableProgs">
         <f:selectItems value="#{pc_EditProgrammeMatrix.availableProgrammes}"/>
    </h:selectManyListbox>

    (cutdown) Page code to populate the Select Items:
    public ArrayList<SelectItem> getAvailableProgrammes(){
        ProgrammeLite currentProgrammeLite;
        Iterator avilableIterator = getProgrammeHandler().getAllProgrammeLites().iterator();
     
        this.availableProgrammes.clear();
     
        while(avilableIterator.hasNext()){
            currentProgrammeLite = (ProgrammeLite) avilableIterator.next();
            this.availableProgrammes.add(new SelectItem((Integer) currentProgrammeLite.getID(), currentProgrammeLite.getTitle()));		
        }
     
        return this.availableProgrammes;
    }

    This populates the SelectItems, allowing me to iterate through them, printing their value and label to the console. When returned to the JSF, the selectManyListbox is empty.


    To test this, I manually populated the SelectItems array:
    public ArrayList<SelectItem> getAvailableProgrammes(){
        this.availableProgrammes.add(new SelectItem((Integer) 1, "Hello");
        this.availableProgrammes.add(new SelectItem((Integer) 2, "World");
        return this.availableProgrammes;
    }

    This works correctly and displays the SelectItems.

    I think this may have something to do with the JSF life cycle, but I am unable find a solution.

    Any suggestions, help, or appropriate work around would be very much appreciated. Let me know if you need more info, etc.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JSF: Dynamically building SelectItems

    What's the scope of your bean? I.e. @SessionScoped?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: JSF: Dynamically building SelectItems

    The pagecode (EditProgrammeMatrix) is request; and it is retreiving data from ProgrammeHandler, which is session.

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: JSF: Dynamically building SelectItems

    It turned out to be a JSF Lifecycle issue. I have resolved it by creating a new page dedicated to organizing the programmes. Not ideal, it at least it works.

Similar Threads

  1. Building your own Date class.
    By ShaunB in forum Java SE API Tutorials
    Replies: 5
    Last Post: October 25th, 2011, 05:37 PM
  2. Need help building a simple calculator
    By zigma in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 14th, 2011, 01:13 AM
  3. Building your own Date class.
    By ShaunB in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 21st, 2011, 06:11 PM
  4. help building up GUI for poker game
    By Pencil in forum AWT / Java Swing
    Replies: 5
    Last Post: October 26th, 2010, 02:53 PM
  5. Building a Menu List
    By websey in forum AWT / Java Swing
    Replies: 1
    Last Post: November 15th, 2009, 10:34 AM