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
– Evilmaax
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()";
– rodrigo.0911
Managed to resolve, thanks for the attention :D
– rodrigo.0911
There’s no point in you calling
atualizarSaldo()
inside the for every time you add a line. Call only once after the loop ends.– Evilmaax
that good. Create an answer showing how you did to resolve and post here. It will help other people.
– Evilmaax