tags:

views:

384

answers:

2

I have a table model that is populated by live (external) data source that updates every xx seconds. The data is displayed in a JTable.

The user can override the data in the table cell through an celleditor that extends the AbstractCellEditor. If the user clicks away, the code checks to see if the new value equals the value in the table model. If it doesn't, it assumes it's an override.

The problem is, say a you click on a field, don't change the value, and external data source updates the table model in the meantime, the code thinks the user set an override using the old value rather than just cancelling the override.

What is the standard logic and implementation to do this?

A: 

Your table model should maintain state that indicates if a table cell was overriden on a cell-by-bell basis. If you dont do that, you will also run into the problem where a user has overriden a cell and the live data comes in and changes to the same value that the use has put in. You will then cancel that override.

You can populate this state in an overriden setValueAt(Object,int,int) method in your TableModel. This method will get called by the JTable when your AbstractCellEditor finished editing.

akf
A: 

How do I detect whether a user has made a change or not?

I made a class which I call TableCellListener that listens for changes made via a cell editor. It takes a copy of the value before the cell starts editing and compares it to the value of the cell after it stops editing. It sounds like it might be what you want.

Another solution might be to check if the table is currently editing the cell before you update the TableModel in the background. Maybe you popup a dialog giving the user a chance to accept the update or cancel it.

camickr