What is the best way to listen for changes in JTable cell values and update database accordingly?

Igor picture Igor · Oct 10, 2012 · Viewed 36.5k times · Source

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?

Answer

trashgod picture trashgod · Oct 10, 2012

You can implement the CellEditorListener interface, as shown in this example. Note that JTable itself is a CellEditorListener.

It may also be convenient to terminate the edit when focus is lost, as shown here:

table.putClientProperty("terminateEditOnFocusLost", true);

More Swing client properties may be found here.