0

I want to display array elements in the "jTextArea1".My 2D array is not a completely filled one.it is like this. row_count = 4 and column_count =5.

2D array:

    4 8 8 4 5
    2 7 5 6 
    6 4 1 8
    9 5 5 2

for(int r=0;r<row_count;r++)
{
    for(int c=0;c<column_count;c++)
    {
        jTextArea1.setText(Integer.toString(newArray[r][c]));

    }
    System.out.println("\n\n");
}

Out put of the above code was just zero. what is the error of this code? thanks.

#

 0
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • `System.out.println("\n\n");` is the only line which will execute. –  May 12 '17 at 06:57
  • What is r and c? Or what are column and rowcount'? – Stimpson Cat May 12 '17 at 06:57
  • Possible duplicate of [How can I evenly space out 2d array in JTextArea without \t?](http://stackoverflow.com/questions/28444279/how-can-i-evenly-space-out-2d-array-in-jtextarea-without-t) – farhan May 12 '17 at 06:58

2 Answers2

2

you overwrite every time the text by doing:

jTextArea1.setText(Integer.toString(newArray[r][c]));

consider using the helpfull Arrays method deepToString and you will get rid off the nested loops :)

System.out.println(Arrays.deepToString(x).replace("[", "").replace("]", ""));

so you can use:

jTextArea1.setText(Arrays.deepToString(newArray).replace("[", "").replace("]", ""));
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

You overwrite the TextArea input with setText(). Try TextArea1.append().

Phillip K
  • 237
  • 1
  • 8