How to update JTextArea automatically

0

I created a program that has a for, this generates different values, what I need is to print the values in a JTextArea as 8 values are generated. I've done the following:

int tp=2;
int pop[][] = new int[tp][8];

Random ran = new Random();
for(int i=0; i<tp; i++){
     String texto="";
     for(int j=0; j<8; j++){
           int valor = ran.nextInt(2);
           pop[i][j] = valor;
           texto += valor+", ";
     }
     txtArea.setText(txtArea.getText()+"\n"+"Valor: "+texto);
}

Theoretically, you should set the value to JTextArea , but this does not happen.

I do not know how to solve this problem.

    
asked by anonymous 15.11.2017 / 20:39

1 answer

0

Try this:

int tp = 2;
int colunas = 8;
int pop[][] = new int[tp][colunas];

Random ran = new Random();
StringBuilder texto = new StringBuilder(tp * (colunas + 8));
for (int i = 0; i < tp; i++) {
     texto.append("Valor: ");
     for (int j = 0; j < colunas; j++) {
           int valor = ran.nextInt(2);
           pop[i][j] = valor;
           texto.append(valor);
     }
     texto.append('\n');
}
txtArea.setText(texto.toString());
    
15.11.2017 / 21:08