• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to best populate a JTable question

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an JPanel object containing a JTable. The JPanel will be fed information that will be used to populate the JTable. My question is how best to populate the JTable each time it gets new information. Should it
recreate the model each time it gets new data. or just use the setValueAt
method?

Thanks.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are changing some values in the JTable you can use table.setValueAt. If you want to add rows or columns then there are methods for this in DefaultTableModel. To find this class from the JTable api scroll down to the method getModel and see that the method return type is TableModel. Follow the link to the TableModel interface and see All Known Implementing Classes: AbstractTableModel. Follow this link to AbstractTableModel and see Direct Known Subclasses: DefaultTableModel. This is a useful and easy–to–use TableModel if you need to make changes to your JTable data.

If you need to update or change all of the JTabel values you can replace the model vector with setDataVector.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!