This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Saving and Loading content of JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Saving and Loading content of JTable" Watch "Saving and Loading content of JTable" New topic
Author

Saving and Loading content of JTable

Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
Hi, i have a simple program that is responsible for adding new rows to JTable and then saving them or loading them from file. Saving to file works fine, loading also, but what i want to achieve, when loading, is to add loaded rows to existing one, not replacing them, what is happening now and i don't like this behavior. One more thing which concerns me is that when i have loaded rows from file i can add new rows, but the cell editors aren't working in this case JComboBoxes, and i don't know why. Any advices are welcome.
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
I posted this in Beginning Java because i'm new to Java programming, i learn Java from, about, one month, so everything is on the beginning level for me, but i will keep this in mind to carefully choose my forum next time.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
what i want to achieve, when loading, is to add loaded rows to existing one, not replacing them,


Don't use the setDataVector() method, that replaces all the data in the model. Instead, loop through the Vector that you just created and use the addRow(...) method to add the data to the existing TableModel.

when i have loaded rows from file i can add new rows, but the cell editors aren't working in this case JComboBoxes


Whenever you use methods like setDataVector() the TableColumnModel of the JTable is recreated so you lose your custom renderers and editors. Using the approach from above should also solve this problem.
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
I changed my code as it was suggested by Rob and it works fine for me, now i can add loaded rows from files to existing ones without replacing them and after loading i'm able to use cell editors either while adding new rows. Thank you for solving my problems.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
Thank you for posting the working code, it may well help someone else in the future.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Saving and Loading content of JTable
 
Similar Threads
Counting occurrence of an item in JTable
JTable selected row problem
Get creation time for adding a new row in jtable
JTables
adding a row to a jtable by pressing a key