### java

#### how to calculate total of column in JTable but started at particular index

```I have Amount column in JTable and one 'Total' and 'Final Total' button. So if I put 2 and 3 in amount column and hit 'Total' then i got 5 in another cell of same column.(My programm is worked fine till this step ) Now I want to insert 3 and 4 below 5. I inserted and hit 'Final Total' then want to display only 12(5+3+4) in same column(Amount) at the last row. (5 is the answer which i got hitting total).
Here I paste my code of 'total' and 'Final Total' button. I am not sure about for loop using in 'Final Total' button. So please give solution for it.
JButton total = new JButton("Total");
{
public void actionPerformed(ActionEvent ae)
{
flag=1;
Object source = ae.getSource();
if (source == total)
{
model.addRow(new Object[]{new Integer(0),"TOTAL",new Integer(1), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
for( i = 0; i < model.getRowCount(); i++)
{
amount = Double.parseDouble(model.getValueAt(i, 6).toString());
sum = new Double(sum+amount);
}
model.setValueAt(sum,model.getRowCount()-1,6);
}
JOptionPane.showMessageDialog(null,"If you pressed Total then you can't select 'Add Row' button ");
model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
model.addRow(new Object[]{new Integer(0),"",new Integer(0), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
}
});
JButton btnFinalTotal = new JButton("Final total");
{
public void actionPerformed(ActionEvent ae)
{
flag=2;
Object source = ae.getSource();
if (source == btnFinalTotal)
{
d= Double.parseDouble(model.getValueAt(i,6).toString());
System.out.println("Current value of d is " +d);
model.addRow(new Object[]{new Integer(0),"FINAL TOTAL",new Integer(1), new Double(0.0),"",new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)});
for(i=0;i>d;i++)
{
amount = Double.parseDouble(model.getValueAt(i, 6).toString());
finalamount= new Double(d+amount);
}
model.setValueAt(finalamount,model.getRowCount()-1,6);
}
}
});
public void tableChanged(TableModelEvent e)
{
if (e.getType() == TableModelEvent.UPDATE)
{
System.out.println("Cell " + e.getFirstRow() + ", "
+ e.getColumn() + " changed. The new value: "
+ table.getModel().getValueAt(e.getFirstRow(),
e.getColumn()));
int row = e.getFirstRow();
int column = e.getColumn();
if(flag==1)
{
if(column == 6)
{
// taxonamt= Double.parseDouble(model.getValueAt(row, 6).toString());
sum=Double.parseDouble(model.getValueAt(row, 6).toString());
// Double total = new Double (sum*(taxonamt/100));
Double total = new Double (sum);
model.setValueAt(total,model.getRowCount()-1,7);
}
}
}
}
```
```You need to place some kind of "marker" into your data which designates a row as a "total" row, so you can find the last "total" row and calculate the rows below it.
Personally, I'd start with a plain old java object (POJO) which encapsulate the functionality you're aiming for and build the table model around it```

Encrypt Message

code
soft
python
ios
c
html
jquery
cloud
mobile