• 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

update problem with JTable

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, How Can I do refresh my model in a JTable? I was trying to refresh the rows of my JTable with a new data, but I can't do this for any reason.
Can I do remove all rows and insert it again? in a simple way?

Somebody have any code that help me?

Thanks
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can change the data in the model's "setValueAt(Object value, int row, int col)" method...you update the value and fire an update event:

data[row][col] = value;
fireTableCellUpdated(row, col);
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

To remove all rows and insert new ones (for example, after user performs a search and you want to display the results), just reset the table model for your JTable.

For example, consider your JTable is called mainTable, and your search returns a new TableModel, called tableModel. To refresh the table with the new data, just do:

 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jared Chapman:
To refresh the table with the new data, just do:



You shouldn't replace the tableModel. You should inform the tableModel that you want to clear its data collection and then add the new data. When you clear the collection use fireTableDataChanged to make the table redisplay.

Completely replacing the model may work in simple cases, but you could lose other information known to the model in more complex cases.
 
Jared Chapman
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good call, Peter.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In practice i guess that we should clear the table and add the new rows.

However for the assignment, since it is a reasonably simple implementation, I have created and added a new table model. Will I be penalized for this.

Appreciate your comments.. thanks.
 
Let's go to the waterfront with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic