What is the best way to listen for changes in JTable cell values and update database accordingly?
Solution 1
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.
Solution 2
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:
final JTable table = new JTable();
table.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
TableCellEditor tce = table.getCellEditor();
if(tce != null)
tce.stopCellEditing();
}
});
Igor
Updated on February 27, 2020Comments
-
Igor about 4 years
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 triedTableModelListener
and overridingtableChanged
, but it fires only when I click away (click on another row) after I have edited a cell.Any other way to do this?
-
mKorbel over 11 yearsI think that is possible to add Listener, everything depends of your XxxCellEditor, but safer way could be waiting after cell is validated on StopCellEdit (the same with MsExcell, without VBA/VBE), otherwise you have in risk
-
Igor over 11 years@mKorbel what kind of listener? I already tried TableModelListener.
-
mKorbel over 11 yearsnot to add proper listener to the JComponent that representing Editor,
-
MadProgrammer over 11 yearsIf possible, I'd try and wait till
setValueAt
on the table model is called, personally
-
-
Igor over 11 yearsThanks, this looks promising. I'll try it out. But, what if the program is closed before focus is lost?
-
Nick Rippe over 11 yearsUnless it's a natural closing (not killed by the task manager or someone using System.exit in the code), I'm 90% sure the focus lost adapter will fire. If not there's also the
WindowListener
that listens for the window closing. -
Nick Rippe over 11 years+1 ooo... I like. Too bad these properties are annoying/difficult to find documentation on.
-
Igor over 11 yearsNope, it doesn't fire. But, I stop Cell Edit on app exit :) for all tables. I used @trashgod's simpler code to stop the editng when focus is lost and I now I just can't decide if I should save all changes on exit, or use the table model listener. I'm sure both will work now, but, which answer do I choose? I used trahsgod's code to sop cell editing and your idea to stick with not commiting everytime a letter is typed in.
-
Nick Rippe over 11 yearsStrange - It's seems I need to be more than 90% sure about things. Good to know!
-
trashgod over 11 years@Igor: For reference,
java.util.Preferences
makes a (platform-specific) best effort to record the last valueput()
. -
Igor over 11 yearsDo you think it's a good idea to use
TableModelListener and
tableChanged()`? -
trashgod over 11 yearsOut of context, good is hard to measure. Try to cover all normal exit paths and use a UPS.
-
Igor over 11 years@trashgod And how about
JTextArea
/JTextPane
? Is there an event listener similar to this, where I don't have to useDocumentListener
and update the database every time a character is entered? An event that fires on "JTextArea
end edit" if what I'm after. -
trashgod over 11 yearsYou could stash a working copy of the content in
java.util.Preferences
, as a hedge, and clear it on database commit.VK_Enter
is bound toinsert-break
, so you'd need another gesture. -
mKorbel over 10 yearsmap KyeBindings not KeyListener