I am having the following issue: I have a JTable with its on tablemodel sitting on a JFrame. The tablemodel is backed up with an own hashmap to store the contents. The table has two columns where the right one is editable.
Normally the user changes some value on the right then presses the enter button which fires the tabledatachanged event that calls my saving function. Then the frame can be closed.
However, some users just simply edit the cell and then they close the window without pressing enter so I get no chance to save the table. I know how to write events when the frame is about to be closed, but I do not know how to retrieve the content of the "unfinished" editing while also finalizing that edit.
I guess it has something to do with the celleditors, I even tried with the table.getCellEditor() that should return the active one but instead it returns null.
Thanks for the help!
The most simple measure is to configure the table to do its best effort when loosing focus:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Unfortunately, there are contexts when its best effort is not good enough (f.i. when the user clicks the close button of a frame). In those, there is no way but to hook into each and every lane that might loose the edit and manually force the edit to stop
if (table.isEditing()) {
boolean stopped = table.getCellEditor().stopCellEditing();
if (!stopped) {
// here goes error handling and/or cancelling the edit
}
}
From your description of the problem,
some users just simply edit the cell and then they close the window without pressing enter
I'm slightly surprised the the first is working, would have expected that you needed doing the second in a WindowListener.
See also Rob's blog entry