java 是否可以在 jtable 中编辑数据并保存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2743236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to edit data in jtable and save it?
提问by firestruq
I would like to know if it's possible to view my values through JTable and then edit them through there?
我想知道是否可以通过 JTable 查看我的值,然后通过那里编辑它们?
回答by Michael Borgwardt
Sure it's possible just have TableModel.isCellEditable()return true, and if necessary set a TableCellEditor.
当然有可能只有TableModel.isCellEditable()return true,如有必要,设置一个TableCellEditor.
回答by gmhk
isCellEditable(int row, int col)This method determines which rows and columns the user is allowed to modify. Since this method returns a Boolean, if all cells are editable it simply returns a true. To prevent a JTable from editing a particular column or row value, it returns a false from this method. The following code enables only column one to display while allowing the rest of the columns to be modified.
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)
When the user makes changes to an editable cell, the Table Model is notified via this method. The new value, as well as the row and column it occurred in, is passed as arguments to this method. If the original data is coming from a database, this method becomes important. As you'll see, data retrieved from a database is held locally within the Table Model, usually as vectors. When the user changes a cell value in a JTable, the corresponding data in the Table Model isn't automatically changed. It's your responsibility to add code in this event to ensure that the data in the Table Model is the same as the data in the JTable. This becomes important when code is added to update the database. The following code updates the data (held in an array of objects) in the Table Model with the new value that the user just entered in the JTable.
当用户对可编辑单元格进行更改时,会通过此方法通知表模型。新值及其出现的行和列作为参数传递给此方法。如果原始数据来自数据库,则此方法变得很重要。正如您将看到的,从数据库中检索到的数据本地保存在表模型中,通常作为向量。当用户更改 JTable 中的单元格值时,表模型中的相应数据不会自动更改。您有责任在此事件中添加代码以确保 Table Model 中的数据与 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();
回答by Arivu2020
Yes it is possible.Basically the jtable is editable.you can check through the TableModel.isCellEditable() method. After editing it you can store the table value in the two dimensional array and store in database. int i; int j;
是的,这是可能的。基本上 jtable 是可编辑的。您可以通过 TableModel.isCellEditable() 方法进行检查。编辑后,您可以将表值存储在二维数组中并存储在数据库中。国际我; 国际 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();
}
}

