我想知道是否可以通过JTable查看我的值,然后在那编辑它们吗?
最佳答案
isCellEditable(int row,int col)
此方法确定允许用户修改哪些行和列。由于此方法返回布尔值,因此,如果所有单元格都是可编辑的,则只需返回true。为了防止JTable编辑特定的列或行值,它从此方法返回false。以下代码仅允许显示第一列,而允许修改其余的列。
// Make column one noneditable
while allowing the user to edit at
all // other columns.
If (col == 1){
return false;
}
else{
return true;
}
public void setValueAt(Object value, int row, int col)
当用户对可编辑单元格进行更改时,将通过此方法通知表模型。新值及其发生的行和列将作为参数传递给此方法。如果原始数据来自数据库,则此方法很重要。如您所见,从数据库中检索到的数据通常作为矢量保存在表模型中。当用户更改JTable中的单元格值时,表模型中的相应数据不会自动更改。在这种情况下,您有责任添加代码以确保表模型中的数据与JTable中的数据相同。当添加代码以更新数据库时,这变得很重要。以下代码使用用户刚刚在JTable中输入的新值来更新表模型中的数据(保存在对象数组中)。
// Update the array of objects with
// the changes the user has just entered in a cell.
// Then notify all listeners (if any) what column
// and row has changed. Further processing may take place there.
rowData[row][col] = value;
fireTableDataChanged();