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

Thread: how to print for-loop output to JTextArea

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default how to print for-loop output to JTextArea

    This code simply prints the even numbers from 0 to 10 which are

    0,2,4,6,8,10

    If you change the variables first and last to say 20 and 100 respectively then the result will be the even numbers from 20 to 100.


       [COLOR="Black"]private int first = 0;
       private int last = 10;
     
          for(int n = first; n < last; n = n + 2)
          {
             int nex = (first + n) - first;
             System.out.print(nex +"," );
     
          }
          System.out.print(last);[/COLOR]


    now I have an interface contains a JTextArea. I want to make a for-loop like the one above which enable me to move the output to JTextArea.

    I only know the method setText(String t); but this method only sets the last output from the for-loop to JTextArea which is the case above 10


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: how to print for-loop output to JTextArea

    textarea.setText(textarea.getText() + " " + textToAppend);
    You can also use a StringBuilder or StringBuffer to append all the text in the loop and afterwords call setText() with your builder/buffer string rep. (in loops I'd recommend the use of these objects as opposed to string 'addition')

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: how to print for-loop output to JTextArea

    Quote Originally Posted by copeg View Post
    textarea.setText(textarea.getText() + " " + textToAppend);
    You can also use a StringBuilder or StringBuffer to append all the text in the loop and afterwords call setText() with your builder/buffer string rep. (in loops I'd recommend the use of these objects as opposed to string 'addition')
    Thank You ,, I got it.

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  2. help with loop to print an equliateral asterisk triangle
    By everyone0 in forum Loops & Control Statements
    Replies: 13
    Last Post: October 11th, 2012, 12:37 PM
  3. How can I append text in JTextArea from another class
    By chikaman in forum AWT / Java Swing
    Replies: 2
    Last Post: December 10th, 2009, 09:26 AM
  4. help writeing to a jtextarea from a diffrent class
    By mdstrauss in forum AWT / Java Swing
    Replies: 6
    Last Post: October 6th, 2009, 09:56 AM
  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