0

I have a Jframe, inside the frame i have a textarea.

public void count() {

    int i;
    for (i = 0; i < 99999; i++) {
    System.out.println(i);
    jTextarea.setText("i: " + i);
    }

}

the message displayed in the textarea is only the last number. how to display the system.out message in the textarea

BeyondProgrammer
  • 893
  • 2
  • 15
  • 32
  • check following solution http://stackoverflow.com/questions/5107629/how-to-redirect-console-content-to-a-textarea-in-java – sasankad Dec 12 '13 at 05:06

3 Answers3

2

Currently you're resetting the value in the jTextArea on every iteration. Instead, you need to concat all the messages together and then set it to your TextArea. You can do that using a StringBuilder.

StringBuilder msg = new StringBuilder();
for (i = 0; i < 99999; i++) {
    System.out.println(i);
    msg.append("i: " + i + "\n");
}
jTextarea.setText(msg.toString());
Rahul
  • 44,383
  • 11
  • 84
  • 103
2

Well there isn't any easy way to send the output of System.out.println to the JTextArea, but what you should probably do instead is simply concatenate your output to the JTextArea's text, like this:

jTextArea.append(i + "\n");
Jashaszun
  • 9,207
  • 3
  • 29
  • 57
  • using this method it will flash display all the value at once, how to display it like System.out.println where value display 1 by 1 into the textarea – BeyondProgrammer Dec 12 '13 at 05:11
2

setText does just that, sets the current text value to what ever you specify.

Instead, you could try using append, which will append the text you supply to the end of the JTextArea's Document, for example...

jTextarea.append("i: " + i + "\n");
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366