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: Printing an arraylist of objects in JoptionPane?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Printing an arraylist of objects in JoptionPane?

    Hello,

    I'm writing a program to store my record collection. I'm struggling with a nice way to output the whole collection, however

    relevant code, trying to use a stringbuilder:

    	public static void printPlateSamling(){
    		StringBuilder builder = new StringBuilder(platesamling.size());
    		for(VinylPlate plate : platesamling){
    			builder.append("\n");
    		}
    		//this doesn't work
    		JOptionPane.showMessageDialog(null, builder.toString());
    	}

    Key: platesamling = arraylist of record objects
    vinylplate = record object

    Could anyone help me out with this?

    Regards,
    VikingCoder


  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: Printing an arraylist of objects in JoptionPane?

    The only thing you ever append to the StringBuilder is a bunch of new lines. What did you expect to happen?
    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
    Oct 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing an arraylist of objects in JoptionPane?

    Quote Originally Posted by KevinWorkman View Post
    The only thing you ever append to the StringBuilder is a bunch of new lines. What did you expect to happen?
    Hah, well, since I didn't exactly know what I was doing I was hoping for a miracle.

    That's why I posted the question here. So, do you have a suggestion as to a way to print an arraylist of objects (containing strings, if this is relevant) with JOptionPane? (or a similarly nice-looking output)

    Regards,
    VikingCoder

  4. #4
    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: Printing an arraylist of objects in JoptionPane?

    I would start out by simply printing out what you want to the command line using System.out.println(). What do you want to print out? What is the String representation of a VinylPlate?
    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!

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Printing an arraylist of objects in JoptionPane?

    when you are appending the builder variable, i notice you only apend the "\n" newline and nothing else. You need to append the record to the builder variable:
    i.ei.
    builder.append(plate."something");
    builder.append("\n");
    now I don't know how your record class is define, but if you want to append String, you need to have a string variable in your record class such as (String name)?

  6. The Following User Says Thank You to retsameht For This Useful Post:

    VikingCoder (November 15th, 2012)

  7. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing an arraylist of objects in JoptionPane?

    Quote Originally Posted by retsameht View Post
    when you are appending the builder variable, i notice you only apend the "\n" newline and nothing else. You need to append the record to the builder variable:
    i.ei.
    builder.append(plate."something");
    builder.append("\n");
    now I don't know how your record class is define, but if you want to append String, you need to have a string variable in your record class such as (String name)?
    thanks for your help brother i got it with this

    new code:

    	public static void printPlateSamling(){
     
    		StringBuilder builder = new StringBuilder(platesamling.size());
    		for(VinylPlate plate : platesamling){
    			builder.append(plate.fullnavn + "\n");
    		}
    		//this now works :D
    		JOptionPane.showMessageDialog(null, builder.toString());
     
    	}

    also this is my VinylPlate object

    public class VinylPlate {
     
    	public String artist;
    	public String tittel;
    	public String fullnavn;
     
    	public String toString(){
    		return this.fullnavn;
    	}
     
    }

    I should have posted that earlier. Well, thanks for your help guys!

    Kind regards,
    VikingCoder

Similar Threads

  1. advice needed on storing string into arraylist and printing it out.
    By jong2009 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 23rd, 2012, 10:16 AM
  2. [SOLVED] Printing arraylist contents in Jtextarea
    By Johnny Bravo in forum AWT / Java Swing
    Replies: 4
    Last Post: August 31st, 2012, 08:56 AM
  3. [SOLVED] Printing an ArrayList of user-defined Objects
    By er1111 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 2nd, 2012, 11:06 AM
  4. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM
  5. JOptionPane Question/ Printing
    By 03EVOAWD in forum AWT / Java Swing
    Replies: 2
    Last Post: August 31st, 2009, 09:17 AM