Is it possible to edit data in jtable and save it?
I would like to know if it's开发者_运维问答 possible to view my values through JTable and then edit them through there?
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.
// 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.
// 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();
Sure it's possible just have TableModel.isCellEditable()
return true
, and if necessary set a TableCellEditor
.
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;
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();
}
}
精彩评论