Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to edit data in jtable and save it?

Tags:

java

jtable

I would like to know if it's possible to view my values through JTable and then edit them through there?

like image 372
firestruq Avatar asked Jan 19 '26 01:01

firestruq


1 Answers

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();
like image 106
gmhk Avatar answered Jan 20 '26 14:01

gmhk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!