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

Thread: Printing arraylist contents in Jtextarea

  1. #1
    Junior Member Johnny Bravo's Avatar
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Printing arraylist contents in Jtextarea

    Hi everyone, I am trying to build a program that will estimate download time for a given file size and internet speed. user will use a simple GUI interface with two text field (to input filesize and net speed) and a button labeled calculate that will show the estimated download time in a textarea just below the calculate button.

    I have three classes calculation class for calculation, gui class for user interface and of course the main class.

    after calculation I have stored the result (i.e. day hour minutes and seconds) in a ArrayList with string type.

    problem is Jtextarea won't allow me to print an arraylist with its setText() method. I am new to java programming and I have been stuck for the last two days with this problem. my program would be complete if it were not for this type conversion problem.

    please help


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Printing arraylist contents in Jtextarea

    Create a String, concatenate each ArrayList item to the String, and then send the setText() method the String you created. If you need any help with that, just ask.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member Johnny Bravo's Avatar
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing arraylist contents in Jtextarea

    Quote Originally Posted by aussiemcgr View Post
    Create a String, concatenate each ArrayList item to the String, and then send the setText() method the String you created. If you need any help with that, just ask.
    Thanks for your reply. I have tried what you said here. but there is a problem with this solution. it works fine first time I click the calculate button. but when I click the button second time the output from the first calculation is also added to the second calculation.

    for example the first output was "5 minutes 24 seconds" and the second is "3 minutes 5 seconds". the second output also adds the first output like "5 minutes 24 seconds 3 minutes 5 seconds" as long as the program runs and calculate button is clicked.

    I suppose this happens because strings are immutable. if it were integer I could have written a method to clear the variable. but with string that's simply not possible.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Printing arraylist contents in Jtextarea

    You can clear Strings, just set it to: ""
    Or create a new String each time.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member Johnny Bravo's Avatar
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing arraylist contents in Jtextarea

    I think I have found the solution. instead of using a class variable of string type I chose to use a local string variable in a method. in this was the variable gets destroyed each time execution of that method ends.

Similar Threads

  1. [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
  2. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  3. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 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. Convert contents of JTextArea / JEditorPane to PDF
    By rangarajank in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 30th, 2009, 02:38 PM