Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TableModel and adding rows  RSS feed

 
Pam Ginther
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Morning,
I am working on my first swing application and I am creating a table that will hold detail items from an order. I want to be able to do the methods in the AbstractTableModel but I also need to be able to add a blank row like in DefaultTableModel. The sun javadoc's do not recommend the use of DefaultTableModel. Question: What table model should I use that will allow me all of the capabilities that I need?
Thank you in advance for your time!
Pam Ginther
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun's Javadocs don't recommend using DefaultTableModel? Where did you see that? Assuming it's true, the AbstractTableModel is a good starting point, but you have to create the data structure for holding the cell contents. That could be a vector of vectors like the DefaultTableModel uses. If you're not inserting columns though, maybe an array of Vectors is better, or if you're not worried about mulitple threads, an array of ArrayLists. How you implement an insertRow() method will depend on what data structure you use.
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
extend AbstractTableModel. It's easy to implement the abstract methods. Your data will be a Collection of Orders (vector, hashtable, etc). GetColumnCount will return the number of fields in an order, and GetRowCount will return the size of the order Collection. GetValueAt will return the appropriate field of the appropriate order. You can create a method addOrder, and just put a new order in the collection and call fireTableRowsInserted.
HTH
 
Pam Ginther
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Greg Charles and Eric Moon for responding. In response to Greg's question concerning DefaultTableModel. In the javadocs for JTable "...JTable is now much easier to set up with custom models the DefaultTableModel is less useful..." "...we recommend wrapping it in the methods of the TableModel interface...".
Will the question that you answered be entered as a chance to win the Swing book? I am not really sure where those questions should be submitted. Thanks Again!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!