I'm building and app with multiple JTables
and I need to detect when cell value change occurs so I can update it in the database. I tried TableModelListener
and overriding tableChanged
, but it fires only when I click away (click on another row) after I have edited a cell.
Any other way to do this?
I'm agreeing with @mKorbel - unless all your input is checkboxes and dropdowns, you're going to want to wait until the cell editing is stopped (you don't want to commit to the database every time a letter is typed in a textbox).
If the problem is that it's not committing after focus has gone to another component, add a
FocusListener
that stops editing the table when focus is lost on the table:Example:
I use the enter key so everytime a user hit enter the cell will update.
You can implement the
CellEditorListener
interface, as shown in this example. Note thatJTable
itself is aCellEditorListener
.It may also be convenient to terminate the edit when focus is lost, as shown here:
More Swing client properties may be found here.
This is also handy if you want to stop the editing on an event handler from selection change or save button.