This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to best populate a JTable question  RSS feed

 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!