I have a JTable to enter some numbers ina certain range. When they exceed that range, an exception occurs to show an error. Also when you enter other characters or even you enter nothing.
When those things happen, I want to get the old value back. I have thought to make a listener which warns me when a cell gets focus (I mean when the cell is opened for entering values). Then I would save that value, and if something goes wrong, I have the value ready to be entered again.
I have searched for a while but I haven’t seen the way to do that listener yet. How could I do that (or in a different way)?
Table model (your subclass of
AbstractTableModel) is expected to do the validation.When JTable is updated, it performs a call to
Validate the value there, if it does not match the range, just do not update it in your implementation of the model. After setValueAt() is called, the table re-reads the value from model (by a call to
getValueAt()), so the original content gets back to the table.