How to update Jtextarea automatically

Asked

Viewed 53 times

0

I created a program that has a for, this for generates different values, what I need is to print the values in a JTextArea as 8 values are generated. I did 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 should go setting the value in JTextArea, but that doesn’t happen.

I don’t know how to solve this problem.

  • What is this matrix pop?

  • This matrix is where I will store the 8 values, the number of rows of it is "tp" and the number of column 8

  • Please add a [mcve] because this code has some variables that are not even shown their types.

  • If the goal is to update the component with the loop still running, you will need Swingworker. Take an example: https://answall.com/a/248098/28595

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());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.