Java jTable计算每一行

时间:2016-08-31 11:32:40

标签: java jtable rows

这里有人可以帮我解决如何在jtable中计算行数吗? 假设我有一个包含字段date,description,aCCount,SCount和lost的表,我已经从数据库中加载了四个字段,除了丢失字段,因为我想在运行时计算它,有人可以帮助我吗?这里是图片附件以供澄清

public void cha(){
  ArrayList<Chalsim> list = getChalsim();
  DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
  Object[] row = new Object[4];
  for(int i =0; i<list.size(); i++){
      row[0] = list.get(i).getDate();
      row[1] = list.get(i).getDesc();
      row[2] = list.get(i).getAc();
      row[3] = list.get(i).getSc();

      model.addRow(row);
  }

}

1 个答案:

答案 0 :(得分:0)

您可以扩展AbstractTableModel,然后将其分配给JTable

new AbstractTableModel() {
public String getColumnName(int col) {
    return columnNames[col].toString();
}
public int getRowCount() { return rowData.length; }
public int getColumnCount() { return columnNames.length; }
public Object getValueAt(int row, int col) {
    return rowData[row][col];
}
public boolean isCellEditable(int row, int col)
    { return true; }
public void setValueAt(Object value, int row, int col) {
    rowData[row][col] = value;
    fireTableCellUpdated(row, col);
}

}

然后将其放入JTable

JTable table = new JTable(new MyTableModel());