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

38,740

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();
    }
});
Share:
38,740
Igor
Author by

Igor

Updated on February 27, 2020

Comments

  • Igor
    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 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?

    • mKorbel
      mKorbel over 11 years
      I 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
      Igor over 11 years
      @mKorbel what kind of listener? I already tried TableModelListener.
    • mKorbel
      mKorbel over 11 years
      not to add proper listener to the JComponent that representing Editor,
    • MadProgrammer
      MadProgrammer over 11 years
      If possible, I'd try and wait till setValueAt on the table model is called, personally
  • Igor
    Igor over 11 years
    Thanks, this looks promising. I'll try it out. But, what if the program is closed before focus is lost?
  • Nick Rippe
    Nick Rippe over 11 years
    Unless 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
    Nick Rippe over 11 years
    +1 ooo... I like. Too bad these properties are annoying/difficult to find documentation on.
  • Igor
    Igor over 11 years
    Nope, 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
    Nick Rippe over 11 years
    Strange - It's seems I need to be more than 90% sure about things. Good to know!
  • trashgod
    trashgod over 11 years
    @Igor: For reference, java.util.Preferences makes a (platform-specific) best effort to record the last value put().
  • Igor
    Igor over 11 years
    Do you think it's a good idea to use TableModelListener and tableChanged()`?
  • trashgod
    trashgod over 11 years
    Out of context, good is hard to measure. Try to cover all normal exit paths and use a UPS.
  • Igor
    Igor over 11 years
    @trashgod And how about JTextArea/JTextPane? Is there an event listener similar to this, where I don't have to use DocumentListener and update the database every time a character is entered? An event that fires on "JTextArea end edit" if what I'm after.
  • trashgod
    trashgod over 11 years
    You 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 to insert-break, so you'd need another gesture.
  • mKorbel
    mKorbel over 10 years
    map KyeBindings not KeyListener