Why would you use a Vector A simple List will do the job too (and be more performant)
In my application my TableModel is refreshed after each modification (only bookings) by executing the search again with the last known search criteria. So a deleted record will not mess up the indexes.
For the sake of ease, instead feeding your GUI with a Vector or List from the database, let your database spit out a Map of record numbers and their records (if you want arrangement use a TreeMap else use HashMap). For security reasons, do not be tempted to just spit out your database cache to the GUI (you don't wanna live with the aftermath). Okay. so you make a copy of you cache (if you implemented one). You should have something like this in the database.
Don't ever let your display relate too much with the data from the database. Let the Controller fetch this Map from the database, use it to construct another Model update the model reference in the main GUI class (very important) and apply the new model on the JTable. This way, the View just sees new models and displays them, nothing more. The model will hold the record numbers in the first column and the other data in the rest according to your schema.
If you want to manipulate a record, call getSelectedRow() on the JTable then call getValueAt(selectedRow, 0), cast the result back to int (from most likely, Integer). You now know which record to manipulate. You can use a switch statement to implement your getValueAt() method in the model.
You should have something like this.
Sorted data structures usually come with a performance hit and with this strategy, you do not need a sorted data structure behind the model to get the right record number from your JTable. The only downside of the unsorted data structure used here is that the user sees records that are not arranged (which should not be too annoying I guess). Just document your choice in your choices.txt.
And please, don't ever forget to update the main GUI class' reference to the table model in every search/refresh/record manipulation. Else you end up modifying record 4 instead of record maybe 15 after performing a search that returns maybe 5 records. You get a feel of the headache that can cause, right?
I can't upload complete working code but I think this should help. I'm still waiting for my result. Hoping for the best.
OCPJP 6, OCMJD 6
I've never won anything before. Not even a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss