Search code examples
javaswingjtextarea

JTextArea dynamic name initiazation using array or any other method


please give any solution. i want to use it with dynamic name.

for(i=0;i<10;i++)
{
    JTextArea ta[i]= new JTextArea();
    String s=""+i;
    ta[i].setText(s);
    i++;
}

Solution

  • I guess that what you want to do is rather this:

    // Create an array of JTextArea
    JTextArea[] jTextAreas = new JTextArea[10];
    // Iterate on all the possible indexes
    for(int i=0;i<jTextAreas.length;i++) {
        // Create a new instance of JTextArea for the current index
        jTextAreas[i] = new JTextArea();
        // Set dynamically the text
        jTextAreas[i].setText(Integer.toString(i));
    }