how to show data in a JTextField when pressing a jbutton?

1

How can I get it printed in the JtexField that is txtCodigo when I press the button.

   private void jbGenerarActionPerformed(java.awt.event.ActionEvent evt) {  
    Random1 ran = new Random1();
    ran.llenarMatriz();
    ran.imprimirMatriz();
    txtCodigo.setText(jbGenerar.getText()); }

this is class Random1

private int matriz[][];
private Random random;

public Random1() {
    this.matriz = new int[1][10];
    random = new Random();
}

public void llenarMatriz() {
    try {
        for(int i = 0; i<matriz.length; i++) {
            for (int j = 0; j<matriz.length; j++) {
                matriz[i][j] = random.nextInt(50000);
            }
        }
    } catch(Exception e) {
        System.out.println(e.getMessage());
    }
}

public void imprimirMatriz() {
    for(int i = 0; i<matriz.length; i++) {
        for (int j = 0; j<matriz.length; j++) {
            System.out.print(matriz[i][j]+"\t");
        }
        System.out.println();
    }
}
    
asked by Uli Baeza 22.08.2017 в 21:09
source

2 answers

1

First you would have to pass it to string , because it seems to me that in jtextfield only allowed that to be that, and then setText .

ElString = String.valueOf(matriz[i][j])+"\t";
jTextField1.setText(ElString);

I think it could be something like that, I hope to be helping.

    
answered by 22.08.2017 в 23:16
0

You have not specified what you want to print specifically in the JTextField. I suppose for your code that what you want is to print all the values of the matrix in the textflied. For that I propose something like this:

  • Add to your class Random1 a getter so you can get the array from the class that contains the JTextField:
  • public int[][] getMatriz(){ return matriz; }

  • In the class that handles the button and the textfield:
  • boton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ int[][] matriz = ran.getMatriz(); for(int i = 0;i < matriz[0].length;i++){ txtCodigo.setText(txtCodigo.getText().toString() + "\t" + matriz[0][i]); } } });

    That's it. I hope I have understood your question. If not, specify more please.

        
    answered by 29.04.2018 в 14:45