defaulttablemodel column summing column code example

Example: how do i get DefaultTableModel rows sum in column in java

jTable1.setColumnSelectionAllowed(true);
jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);



public void calculate() {
    calculate(jTable1, jTable1.getSelectedColumn(), jTable1.getSelectedRows());
}

public void calculate(JTable table, int column, int[] rows) {
    int result = 0;
    for (int row : rows) {
        result += Integer.valueOf(table.getValueAt(row, column).toString());
    }
    ((DefaultTableModel) table.getModel()).addRow(new Object[]{result});
}

Tags:

Java Example