更新jtable后如何更新double的值

时间:2017-03-15 21:28:02

标签: java swing jtable double defaulttablemodel

enter image description here我需要一些帮助,在更新JTable后更新double的值,代码应该从列中获取值并将它们组合在一起。

这是获取价值的代码:

double cpusum = 0;
int prow=1;

DefaultTableModel cpuprice = (DefaultTableModel) cpubaskettable.getModel();
for (int i = 0; i < cpuprice.getRowCount(); i++) {
    cpusum = cpusum + Double.parseDouble(cpuprice.getValueAt(prow, 5).toString());

}
DecimalFormat df = new DecimalFormat("#.##");





JLabel cpubaskettotallabel = new JLabel("Total Price: " + df.format(cpusum));

以下是更新JTable的代码:

private void cputobasket(){
    TableModel model1 = cputablebottom.getModel();
    int [] index = cputablebottom.getSelectedRows();
    Object []row = new Object[6];
    DefaultTableModel model2 = (DefaultTableModel) cpubaskettable.getModel();
    for (int i = 0; i<index.length;i++){
        row[0] = model1.getValueAt(index[i], 0);
        row[1] = model1.getValueAt(index[i], 1);
        row[2] = model1.getValueAt(index[i], 2);
        row[3] = model1.getValueAt(index[i], 3);
        row[4] = model1.getValueAt(index[i], 4);
        row[5] = model1.getValueAt(index[i], 5);
        model2.addRow(row);
    }

}

它还应该在删除行时更新,代码为:

private void deletecpufrombasket(){

    int row = cpubaskettable.getSelectedRow();
    DefaultTableModel model2 = (DefaultTableModel) cpubaskettable.getModel();

        model2.removeRow(row);
    }

0 个答案:

没有答案