Solved

Keeping track of JTable changes

Posted on 2006-06-17
10
522 Views
Last Modified: 2012-08-13
Hi,
  I am writing an application that displays database tables in a JTable object.  I am using this dialog to allow the user to make changes to the table.  I am having a hard time designing a way to keep track of user changes.  I want to be able to know when the user has made changes so I can save the data to the database.  

Later down the road I also may implement an undo feature and may want to keep track of the user changes in general.  I have not found much documentation on the web and was wondering if anyone would be able to help?

Thanks for all your help!!
0
Comment
Question by:Brock20
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16928128
Just make a copy of your TableModel
0
 

Author Comment

by:Brock20
ID: 16928150
So I should have a copy of the TableModel and then compare the data?  Wouldn't this take up resourses?  I was trying to think of a way to have the model or the view know that something has changed.  Not sure if that is the proper way to do it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16928164
It depends what you want to do. If you don't need to know what the changes are, but merely that changes did occur, just calculate the hash code before and after
0
 

Author Comment

by:Brock20
ID: 16928186
1.)  How would I calculate the hash code?

2.)  So if I want to know what the changes are you are suggesting to keep two copies of my model?  Is this expensive?  
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 26

Accepted Solution

by:
ksivananth earned 125 total points
ID: 16928928
if you feel having a copy of the model is expensive, just add nonvisible column( flag tracking changes ) in the model. and you are going to update the flag when there is a change in cell, here the drawback is that the flag is going to tell u that the data got changed even if the user had reverted the changes by updating the changed cell again. But this can be worked around!
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
ID: 16929022
>>How would I calculate the hash code?

Iterate the model and add the hashcode of every value

>>Is this expensive?

That would depend on the size of the model
0
 
LVL 1

Assisted Solution

by:FunnyMan
FunnyMan earned 125 total points
ID: 16929891
Hashing's relatively cheap, it's a linear-time algorithm, meaning that it takes about twice as long to run it on a data set that's twice as long.

The better alterantive, though, is to implement TableModelListener and add your class as a listener to the underlying TableModel (via table.getModel().addTableModelListener(this)).  Then you can just watch the TableModelEvents that come past and keep track of them.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 16931027
You shouldn't need two copies of the table model, a TableModelListener as FunnyMan has suggested could easily be used to notify you when a value in the table changes.
When it gets called you can start a thread to perform the required database update.

http://jdj.sys-con.com/read/36636.htm
http://www.oracle.com/technology/products/jdev/howtos/10g/interceptor.html
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now