Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

update problem with JTable

 
Anibal Gimenez Canicoba
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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
 
Santosh Joseph
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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);
 
Jared Chapman
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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:

 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Good call, Peter.
 
Manik Saha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic