JTable Cell Update doesn't work

That's expected behaviour: the edited value isn't committed to the backing model until an explicit user gesture, like f.i. pressing enter or tabbing out or clicking elsewhere in the table ...

One oddity (some call it bug :-) of JTable is that editing isn't by default terminated when transfering focus to the "outside" of the table. To force it doing so, you need to configure it like:

  table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

BTW (unrelated, just for sanity): always fire the most fine-grained event type, here that would be a cellUpdated instead of the dataChanged hammer.


The default update mechanism only changes the model when the cell editor loses the focus. Either tabbing out of the cell or clicking in a different cell will cause the vital "focus lost" event which triggers the model change.

Clicking in the gray area has no effect because there are no active elements there which could process the event - the table ignores the click.

If you want to change this, you need to find an event which tells the computer that the user is "done with editing". How do you know that?

  • You could add an ActionListener (see http://download.oracle.com/javase/tutorial/uiswing/components/textfield.html). It will get triggered when you press RETURN. In the handler, call fireEditingStopped() to trigger the "copy to model" code (see http://download.oracle.com/javase/tutorial/uiswing/components/table.html#editor).
  • You could add a "Save" button below the table and call this code in it's ActionListner:

    if(null != jTable.getCellEditor()) {
        // there is an edit in progress
        jTable.getCellEditor().stopCellEditing()
    }
    
  • You could update the model for each keypress by adding a keypress handler but that might cause the editor to disappear.

[EDIT] Note that adding a "Save" button can disrupt the edit "flow" of users (click table cell, edit, grab mouse, aim, click save, click next cell, go back to keyboard, edit, ...)