How to compare the value of a cell before changing and after changing.

Hey there,

Now I want to implement a JTable. And the requirement is that users can edit a cell in the Table only once. I think there are two possible ways. One is that after user edited a cell, I will set this cell read only. Another way is to compare the vaule before and after the changing. If it is different, then write back the previous value.

But I did not know how to implement in Java. Can anybody help me?

Thanks alot!

Shelly

[466 byte] By [UnshelledShellya] at [2007-10-2 4:02:12]
# 1

inevitably, you will have to save a before copy of the data, what this many times amounts to is that you will need to have 2 identical data structures before you start or some type of locking mechanism for each piece of data.

With 2 copies of the data, you can allow editing if you have the same in both structures.

With locking, once it's been edited--lock it. You still need to keep a before image of the data for just the cell you are working on.

You will also need some type of commit/rollback logic for "incomplete" edits.

morgalra at 2007-7-15 23:24:36 > top of Java-index,Desktop,Core GUI APIs...
# 2

> inevitably, you will have to save a before copy of

> the data, what this many times amounts to is that you

> will need to have 2 identical data structures before

> you start or some type of locking mechanism for each

> piece of data.

Why is this inevitable? I don't see it as inevitable at all. In fact, it's quite simple to implement a TableModel that returns false when isEditable(int, int) is invoked for any cell that has already had setValueAt(Object, int, int) inovked on it, for example. Depending on the requirements and implementation a different methodology may be needed for determining when the edit has taken place, such as a custom editor that notifies the TableModel when stopCellEditing() is invoked. Heck, overriding editingStopped() in JTable to do it might even work.

So, create a TableModel that returns isEditabe(int, int) as true only if the cell hasn't been edited yet. The only problem to solve from there is how you know when it's been edited. I've already given you a few leads on that, it's not particularly hard but depending on your exact implementation it may require different solutions.

kablaira at 2007-7-15 23:24:36 > top of Java-index,Desktop,Core GUI APIs...
# 3

>Re: How to compare the value of a cell before changing and after

>changing.

If you know of a way to compare the value before and after changing without having a copy of "before" and "after", I humbly defer to you.

The op does go on to say that he just wants a simple "if changed" then never let them edit again. You're answer is absolutely appropriate for this, and mine would not be the only path.

morgalra at 2007-7-15 23:24:36 > top of Java-index,Desktop,Core GUI APIs...
# 4

You do not need two copies because you could store a flag structure (a map, array, whatever that parallels the model) in the model that gets set when the value changes.

So, in effect you have your model[width,height] and your modified[width,height] structures. A call to setValueAt should write to the modified structure if the value changes and a call to isCellEditable should then consult the modified structure.

Havig said that, you could also keep an originalValues structure and compare each time isCellEditable is called. Bot have there efficiencies and inefficiencies. When I did this sort of thing I kept an originalValues structure around since I also wanted to be able to revert to the original value under certain conditions

In either case, you will need to create your own TableModel. I suggest that you extend the DefaultTableModel class and write your own isCellEditable method.

mitekea at 2007-7-15 23:24:36 > top of Java-index,Desktop,Core GUI APIs...
# 5

> >Re: How to compare the value of a cell before

> changing and after

> >changing.

>

> If you know of a way to compare the value before and

> after changing without having a copy of "before" and

> "after", I humbly defer to you.

>

> The op does go on to say that he just wants a simple

> "if changed" then never let them edit again. You're

> answer is absolutely appropriate for this, and mine

> would not be the only path.

I disagree with the premise that you need to compare the values to begin with. If you need to compare the values, then absolutely you'll have to have a before and after. However, in this situation I believe all that was required is to know that it had changed, not draw any comparison. If the fact that it changed is all you care about then all you need to bother with is knowing when that change takes place regardless of what the change itself is.

kablaira at 2007-7-15 23:24:36 > top of Java-index,Desktop,Core GUI APIs...