Printing an ArrayList into a JTextArea
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   chris_hampstead
Posted On:   Thursday, January 4, 2007 12:20 PM

Hi there, I'm pretty new to java, so I'm sorry if i'm making some basic errors. I've created a basic ArrayList (called 'ArrayList1'), which outputs fine, but I now need to output the ArrayList's contents into the TextArea of a GUI I've created. Currently, the GUI is entirely contained within one class and I have 4 other java files which are necessary for the ArrayList. I initially tried the code below (also using import statements for the classes necessary for the ArrayList), which didnt work. textarea1 = new JTextArea(ArrayList1); As I said, if I use the ArrayList in a non GUI environment, everything outputs fine, but I don't really have the knowledge to get the ArrayList into my GUI JTextArea. Any help or   More>>

Hi there, I'm pretty new to java, so I'm sorry if i'm making some basic errors. I've created a basic ArrayList (called 'ArrayList1'), which outputs fine, but I now need to output the ArrayList's contents into the TextArea of a GUI I've created. Currently, the GUI is entirely contained within one class and I have 4 other java files which are necessary for the ArrayList.

I initially tried the code below (also using import statements for the classes necessary for the ArrayList), which didnt work.

			
textarea1 = new JTextArea(ArrayList1);


As I said, if I use the ArrayList in a non GUI environment, everything outputs fine, but I don't really have the knowledge to get the ArrayList into my GUI JTextArea.
Any help or pointers would be greatly appreciated, as I cant seem to get any further right now
thanks, Chris.    <<Less

Re: Printing an ArrayList into a JTextArea

Posted By:   Robert_Lybarger  
Posted On:   Thursday, January 4, 2007 06:14 PM

... also, it might be interesting to see what you meant by "outputs fine in a non-GUI environment" so we can help shed some more light on things.

Re: Printing an ArrayList into a JTextArea

Posted By:   WarnerJan_Veldhuis  
Posted On:   Thursday, January 4, 2007 12:50 PM

An ArrayList can contain any type of object. Java has no clue of how to print your objects, other than toString(). So the easiest provedure would be to iterate over the list, and print out whatever you need to print in the JTextArea.

Java 5 syntax:


for ( MyObject o : ArrayList1) {
textarea1.append( o.toString() );
}


Replace the toString() if you don't have one (perhaps make one), and substitute for something useful.
About | Sitemap | Contact