• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Saving and Loading content of JTable

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for posting the working code, it may well help someone else in the future.
 
reply
    Bookmark Topic Watch Topic
  • New Topic