misha
misha

Reputation: 134

JTable content editing

I'm trying to edit the selected column in the selected row with button click like this

contactTable.getModel().setValueAt(
    contactTable.getValueAt(contactTable.getSelectedRow(), 
    contactTable.getSelectedColumn()), 
    contactTable.getSelectedRow(), 
    contactTable.getSelectedColumn()
);

but before saving button click if I don't click in another column edit doesn't work, so what is the reason? and how can I fix it?

Upvotes: 0

Views: 99

Answers (1)

camickr
camickr

Reputation: 324197

The edited value is still in the cell editor, not in the TableModel. The value is automatically saved if you move to another cell in the table.

However, if you click on a button you must manually save the data. This can be done in one of two ways:

  1. In the ActionListener of the button you can manually invoke the stopCellEditing() method of the editor
  2. Add a property to the table to save the edits when the table loses focus.

Check out Table Stop Editing for more information and examples of these two approaches.

Upvotes: 1

Related Questions