使用环境如下:
首先,点击查询按钮,从数据库中提取所有记录填充到Jtable中,
然后,Jtable表格中的单元格都是可以编辑的,编辑单元格后,数据自动保存到Jtable单元格中,并同时更新到数据库中,
最后,点击查询按钮,重新从数据库中提取所有记录填充到Jtable中
public class KpiMetadataMappingTableModel extends AbstractTableModel
{
private Vector<UrmKpiMetadataMappingItem> kpiMetadata = new Vector<UrmKpiMetadataMappingItem>();
private String[] columns =
{
"111", "222", "333", "444", "555", "666", "777", "888", "999", "110"
};
public UrmKpiMetadataMappingItem getRow(int row)
{
return kpiMetadata.get(row);
}
@Override
public int getRowCount()
{
return kpiMetadata.size();
}
@Override
public int getColumnCount()
{
return columns.length;
}
public String getColumnName(int col)
{
return columns[col];
}
@Override
public Object getValueAt(int row, int column)
{
UrmKpiMetadataMappingItem kpiMetadataMappingItem = kpiMetadata.get(row);
switch (column)
{
case 0:
return kpiMetadataMappingItem.getKpiId();
case 1:
return kpiMetadataMappingItem.getDeviceId();
case 2:
return kpiMetadataMappingItem.getAlarmId();
case 3:
return kpiMetadataMappingItem.getParameterId();
case 4:
return kpiMetadataMappingItem.getIsAlarm();
case 5:
return kpiMetadataMappingItem.getDeviceType();
case 6:
return kpiMetadataMappingItem.getPageIndex();
case 7:
return kpiMetadataMappingItem.getParameterIndex();
case 8:
return kpiMetadataMappingItem.getBoardName();
case 9:
return kpiMetadataMappingItem.getManufacture();
}
return "";
}
public boolean isCellEditable(int row,int col)
{
return true;
}
public void setValueAt(Object newValue,int row,int col)
{
// 这里边的代码如何写
代码越详细越好
?????
}
public Vector<UrmKpiMetadataMappingItem> getData()
{
return kpiMetadata;
}
}
------解决方案--------------------
- Java code
@Overridepublic void setValueAt(Object newValue,int row,int col) { UrmKpiMetadataMappingItem item = kpiMetadata.get(row); String val = String.valueOf(newValue); switch(column) { case 0 : item.setKpiId(Integer.parseInt(val)); break; case 1 : item.setDeviceId(Integer.parseInt(val)); break; // ... case 8: item.setBoardName(val); break; // ... default : throw new IndexOutOfBoundsException(); } updateRecordInDatabase(item); fireTableDataChanged();}