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

Thread: How to display ArrayList in jTextArea?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to display ArrayList in jTextArea?

    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?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to display ArrayList in jTextArea?

    What type of objects does your ArrayList contain?

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to display ArrayList in jTextArea?

    Class objects
    ArrayList<MyClass> = new ArrayList();

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: How to display ArrayList in jTextArea?

    Quote Originally Posted by Prog View Post
    jTextArea.setText(myArrayList);
    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.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to display ArrayList in jTextArea?

    Interesting. Perhaps you could write a toString() method for MyClass and use that to display the MyClass objects as Strings in the JTextArea.

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to display ArrayList in jTextArea?

    Yes Greg! I got it now!
    What I did is the following:

    jTextArea.setText(myArrayList.toString());

    Works like a charm

    Thank you!

  7. #7
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: How to display ArrayList in jTextArea?

    Quote Originally Posted by Prog View Post
    jTextArea.setText(myArrayList.toString());
    Yes, it works. But you are a bit limited by the format imposed by toString() of ArrayList.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

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

    Default Re: How to display ArrayList in jTextArea?

    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.

  9. #9

    Default Re: How to display ArrayList in jTextArea?

    The method append(String) in the type JTextArea is not applicable
    for the arguments (ArrayList.Account.TransactionObject>)

Similar Threads

  1. ArrayList weird display error
    By PissMaster in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2013, 04:42 AM
  2. JTextArea display button
    By lf2killer in forum Object Oriented Programming
    Replies: 3
    Last Post: November 21st, 2012, 10:06 AM
  3. [SOLVED] Printing arraylist contents in Jtextarea
    By Johnny Bravo in forum AWT / Java Swing
    Replies: 4
    Last Post: August 31st, 2012, 08:56 AM
  4. Help with image display on JTextArea
    By mozart66 in forum AWT / Java Swing
    Replies: 4
    Last Post: May 18th, 2012, 09:35 AM
  5. Trying to display text from file in JTextArea. Please Help.
    By rjdelight in forum Java Theory & Questions
    Replies: 7
    Last Post: June 29th, 2011, 05:10 AM