Friday, January 11, 2013

AbstractTableModel not Firing setValueAt

I had a friend come to me today about a bug found in one of his applications.  He had an issue when the user was editing cells in a JTable the user didn’t click out of the cell for the setValueAt method to be called prior to selecting the OK button to close the dialog.  This made the user’s last change not take effect in their database. 

The solution to this is that in the event listener or action for the button you need to get reference to the table and ask the table whether isEditing() is true.  Then you would get the current cell editor and after validating the last input value either call stopCellEditing() to commit the change or call cancelCellEditing() to undo the value.

if(table.isEditing()){
table.getCellEditor().stopCellEditing();
}
This would always commit the new value.

No comments:

Post a Comment