• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is a good way to get a value before and after it is changed in a JTable cell  RSS feed

 
Jon Swanson
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class that has two basic methods, add and remove. Now I am trying to use this class where the input is a JTable. The basic workflow is simple, let's say a row has three columns, with the values 1, 2 and 3, respectively. I would call myClass.add(1,2,3). The user could then do any of the following:

1. Change a value. Let's say 1 is changed to 2. I would like to run myClass.remove(1,2,3) followed by myClass.add(2,2,3)
2. Remove a row. I would like to run myClass.remove(1,2,3)
3. Add a row. Let's say a row was added with the values 3,4,5 I would like to run myClass.add(3,4,5)

2 and 3 seem straightforward. I am getting hung up on item 1. I'm not sure where (if anywhere) I can get an event that knows what the value was before and after the change.

I tried to do this-


with the idea that I am happy with the default behavior, I just want to intercept the table before it updates. This doesn't work, not to mention the kludge of hard-coding my model, since I wasn't sure how to get the table or model reference. I can changed the first row repeatedly and I get a table changed event from my table model listener, but no value changed event. I only see that event if I click on a different row. By the time I get the table changed event, as best I can tell, the value is already changed.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your TableModel knows when a cell was changed. That's because its "setValueAt" method is called to change the cell.

So if you have access to that code, you could modify the setValueAt method to do your before-and-after processing. Or alternatively you could add a TableModelListener to the TableModel... although when I look at what you can get from that, it looks like you just get a notification that the cell was changed. So that probably wouldn't be helpful, but you could try it out and see what really happens; I haven't used it myself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!