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:
Code :
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
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?
Re: Printing an arraylist of objects in JoptionPane?
Quote:
Originally Posted by
KevinWorkman
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
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?
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)?
Re: Printing an arraylist of objects in JoptionPane?
Quote:
Originally Posted by
retsameht
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:
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
Code :
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