setText method does not insert the text correctly within a repeat loop

Asked

Viewed 292 times

0

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

Only when it arrives in Textset, it does not show the 10 textSet that was to show, but only the last one. Because this happens and how to fix it?

Here 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                   
}
  • This code is not executable. Edit the question and provide a [mcve], so it becomes easier to run the problem and suggest something.

  • put in the code

1 answer

1


Only displays the latter because the method setText does not concatenate text if the field already has one, it simply replaces it. To concatenate text into JTextArea, you must use the method 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 added a line that concatenates a line break, so that the display is correct.

See working:

inserir a descrição da imagem aqui

Browser other questions tagged

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