Error, Sum of values in a Jtable

Asked

Viewed 32 times

0

Good evening guys, I’m trying to implement a method that adds up the values of the lines of a JTable, but is always returning 0...

Follow my Code.

    private void loadData() {
    List<ValoresEntity> list = this.mValoresBusiness.getList();

    String[] columnNames = {"Nome", "Valor de Entrada", "Valor de Repetição", "Id"};
    DefaultTableModel model = new DefaultTableModel(new Object[0][0], columnNames);

    for (ValoresEntity valoresEntity : list) {

        Object[] o = new Object[4];
        o[0] = valoresEntity.getName();
        o[1] = valoresEntity.getValue();
        o[2] = valoresEntity.getValuefinal();
        o[3] = valoresEntity.getId();

        model.addRow(o);
        atualizarSaldo();

    }

    this.tableValores.clearSelection();
    this.tableValores.setModel(model);

    this.tableValores.removeColumn(this.tableValores.getColumnModel().getColumn(3));


}

public void atualizarSaldo(){

    double Rsoma = 0;

    for(int i = 0; i < this.tableValores.getRowCount(); i++){

        Rsoma += Double.parseDouble(this.tableValores.getValueAt(i, 2).toString());

    }
    labelAValue.setText("R$" + Rsoma);

    }
}
  • The code you posted was with a line break error, I believe it was when copying paste here in the OS, but it doesn’t hurt to check

  • Hi, yes, it was copying and pasting, I did a test now, and I created a button to make the sum, and it works normally..., I believe the error is in the part that I call the "updateAld()";

  • Managed to resolve, thanks for the attention :D

  • There’s no point in you calling atualizarSaldo() inside the for every time you add a line. Call only once after the loop ends.

  • that good. Create an answer showing how you did to resolve and post here. It will help other people.

1 answer

0

So, guys, for those of you with the same problem... I decided to call the function "updateSaldo();" in my Public Main(), before that I was trying to call it so I added a line... I’m gonna leave Cod all here so we can get a better understanding.

public Main() {
    this.setContentPane(rootPanel);
    this.setSize(800, 450);
    this.setTitle("Controle de Digitação (Rodrigo Alves v1.0)");

    this.mValoresBusiness = new ValoresBusiness();

    super.defaultConfiguration();

    this.setEvents();

    buttonNew.setMnemonic(KeyEvent.VK_N);

    this.loadData();
    atualizarSaldo(); // Resolvi chamando ele aqui.
}

private void setEvents() {
    this.buttonNew.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new Valores();
            dispose();
        }
    });

    this.buttonRemove.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO
        }
    });

    this.buttonEdit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO
        }
    });
}

private void loadData() {
    List<ValoresEntity> list = this.mValoresBusiness.getList();

    String[] columnNames = {"Nome", "Valor de Entrada", "Valor de Repetição", "Id"};
    DefaultTableModel model = new DefaultTableModel(new Object[0][0], columnNames);

    for (ValoresEntity valoresEntity : list) {

        Object[] o = new Object[4];
        o[0] = valoresEntity.getName();
        o[1] = valoresEntity.getValue();
        o[2] = valoresEntity.getValuefinal();
        o[3] = valoresEntity.getId();

        model.addRow(o);
        // Eu estava chamando ele aqui.

    }

    this.tableValores.clearSelection();
    this.tableValores.setModel(model);

    this.tableValores.removeColumn(this.tableValores.getColumnModel().getColumn(3));

}

public void atualizarSaldo(){

    double Rsoma = 0;
    for(int i = 0; i < this.tableValores.getRowCount(); i++)
        Rsoma += Double.parseDouble(this.tableValores.getValueAt(i, 2).toString());
    labelAValue.setText("R$" + Rsoma);
}

I hope it helped :)

Browser other questions tagged

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