The setText method does not insert the text correctly inside a repeat loop

0

I created a method that shows a loop of repetition, and inside it I get the product of the variable set by the counter, like a table and such.

But when it arrives in TextSet, it does not show the 10 textSet that was to show, but only the last one. Why does this happen and how do I fix it?

Here is the code:

public class Tabuada extends javax.swing.JFrame {
    String numero;
    int valor,contador=0;

    public Tabuada() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        camponumero = new javax.swing.JTextField();
        botaomostrar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        textomostra = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("TABUADA");

        jLabel2.setText("Numero");

        botaomostrar.setText("Mostrar");
        botaomostrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaomostrarActionPerformed(evt);
            }
        });

        textomostra.setColumns(20);
        textomostra.setRows(5);
        jScrollPane1.setViewportView(textomostra);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(106, 106, 106)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(camponumero, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(botaomostrar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(camponumero, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(botaomostrar)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void botaomostrarActionPerformed(java.awt.event.ActionEvent evt) {                                             
             numero = camponumero.getText();
             valor = Integer.parseInt(numero);



           visualiza();




    }                                            

  public void visualiza(){

     while(contador<=10){
         textomostra.setText(this.valor+" x "+this.contador+" = "+valor*contador);
         contador = contador + 1;
     }


  }


    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tabuada().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton botaomostrar;
    private javax.swing.JTextField camponumero;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea textomostra;
    // End of variables declaration                   
}
    
asked by anonymous 04.03.2018 / 03:04

1 answer

1

Only displays the last one because the setText method does not concatenate text if the field already has one, it simply overrides. To concatenate text in JTextArea , you must use the append() ":

 public void visualiza(){

     while(contador<=10){
        String str = this.valor+" x "+this.contador+" = "+valor*contador;
         textomostra.append(str);
         textomostra.append(System.getProperty("line.separator"));
         contador = contador + 1;
     }
  }

I've added a line that concatenates a line break, so the display is correct.

See working:

    
04.03.2018 / 14:04