2010-04-30 44 views

回答

4

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(); 
2

是的,它是possible.Basically JTable的是editable.you可以通过TableModel.isCellEditable()方法检查。编辑完成后,您可以将表格值存储在二维数组中并存储在数据库中。 int i; int j;

String tableData = new String[row count][column count]; 

    for(i = 0; i < row count; i++) 
    { 
     for(j = 0; j < 3; j++) 
     { 
      tableData[i][j] = table.getValueAt(i, j).toString(); 
     } 
    } 
相关问题