How to update JTextArea automatically



  • I've created a program that has one, that generates different values, what I need is to print the values in one 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 it should go setting the value in JTextAreaBut that doesn't happen.

    I don't know how to solve this problem.



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


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 1
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 3
  • 2
  • 2
  • 2
  • 2
  • 2