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 6 of 6

Thread: How do I display 4 items at a time of an ArrayList?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I display 4 items at a time of an ArrayList?

    Hi everyone, I have some basic Java skills and kept experimenting with code and obviously getting stuck with it. I've been recently trying to create an address program but I don't have a clue on how to carry on. In short, I have an ArrayList which contains a list of addresses. They are too many to get displayed so I want to display 4 addresses at a time in a GUI starting from the latest entry. The GUI has one button called previous. Can anyone help me out with a code or some tips.


    Any help will be very appreciated
    Last edited by JavaStruggler; August 18th, 2011 at 07:05 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How do I display 4 items at a time of an ArrayList?

    If it's that urgent, I suggest reading through the Swing tutorial and providing an SSCCE that demonstrates what you've tried.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I display 4 items at a time of an ArrayList?

    H Kevin,
    Thank you for your reply,
    Actually the GUI I created works fine and also the rest of the code, the only problem I have is that I don't know how to display only 4 items at a time instead of the whole list. I would really appreciate any help, even few tips.
    Thank you

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do I display 4 items at a time of an ArrayList?

    Suggested reading: How To Ask Questions The Smart Way

    There are so many ways to do what you wish, point being unless you've given us details on what you've tried we could be throwing darts at a dartboard that does not exist. For instance, you could ditch the 4 at a time and use a JScrollPane, you could use a CardLayout, you could create a class which holds 4 objects of your type, you could iterate through the arraylist 4 at a time, etc...

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I display 4 items at a time of an ArrayList?

    Hi copeg, thank you so much for your tips.
    I don't want to use JScrollPane or CardLayout, I look for a simpler solution. What I wish to do is to iterate through the arraylist, I tried to use an ordinary for loop but the result was that i only managed to display the last 4 objects. Basically I'd like to display 4 items every time I click the 'previous' button until there are no more elements left.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I display 4 items at a time of an ArrayList?

    another approach is to use an index into the array list vs for each.
    Keep track of the current first element. Go forward by adding 4, go back by subtracting 4.
    Get the 4 elements starting at the index's value.

Similar Threads

  1. [SOLVED] Need help with displaying items with my program
    By mike101290 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2011, 08:07 AM
  2. Getting items from a table/checkboxes
    By beth in forum AWT / Java Swing
    Replies: 2
    Last Post: January 5th, 2011, 01:29 PM
  3. Replies: 4
    Last Post: July 21st, 2010, 04:07 PM
  4. misalignment of list items
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: March 15th, 2010, 08:33 AM
  5. display time
    By kalees in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 1st, 2010, 07:40 AM