• Post Reply Bookmark Topic Watch Topic
  • New Topic

adding rows to table dynamically

 
narayana Ganesh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

i am getting records from a database table and dynamically want to add them to table. how could i do this please help me.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the table model's addRow()
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Dunn:
use the table model's addRow()


That's a little misleading as there isn't TableModel.addRow() method. The only way to dynamically add a row to a JTable is by using a concrete TableModel implementation such as DefaultTableModel. This means you need to do something like this:



Depending on your situation, it may be appropriate to create your own TableModel. For example, I recently wrote a ServiceTableModel that was specifically made to display detail lines from a HCFA1500 medical bill. Thus it had convenience methods such as addDetailLine(ServiceDetail data) and populated a new row automatically using the supplied ServiceDetail. If special functionality from the TableModel isn't necessary then instantiating your own DefaultTableModel and passing it to the JTable, then operating on that DefaultTableModel is the simplest way to go.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!