Hello,
I want to display my ArrayList in the text area like this:
jTextArea.setText(myArrayList);
But it gives me an error, because it can only set Strings, and my ArrayList is not a String.
How do I solve this?
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.
Hello,
I want to display my ArrayList in the text area like this:
jTextArea.setText(myArrayList);
But it gives me an error, because it can only set Strings, and my ArrayList is not a String.
How do I solve this?
What type of objects does your ArrayList contain?
Class objects
ArrayList<MyClass> = new ArrayList();
Obviously this doesn't compile. setText requires a String, not other objects. It's up to you to generate a new String that contains, for example, N lines where each line contains a text about an element.
I repeat, you have to do this with some specific code, it requires at least a for cycle, and some string concatenations (better if you use a StringBuffer/StringBuilder).
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
Interesting. Perhaps you could write a toString() method for MyClass and use that to display the MyClass objects as Strings in the JTextArea.
Yes Greg! I got it now!
What I did is the following:
jTextArea.setText(myArrayList.toString());
Works like a charm
Thank you!
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
Just in case you are unfamiliar - and depending upon your needs - a JList or JTable might provide more functionality and neatness when displaying Lists of data.
The method append(String) in the type JTextArea is not applicable
for the arguments (ArrayList.Account.TransactionObject>)