• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Tables?

 
Jacob Steingart
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,
I'm writing some software to (attempt) to help me organize/catalog my home library, and I'm trying to make it so I can dynamically add books to a big JTable of them and info about them. What I want to do is have an ArrayList (or other dynamic array-esque thing) that contains a bunch of Book objects. Each Book will have instance variables such as a title, author, ISBN, etc. How can I make a table/table model that will display this data.

-GUI Newbie
 
Suraj Chandran
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time you add/remove/change books in your table, you just need to call one of the fireXXX methods in the AbstractTableModel.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Table model from ArrayList! My favorite design approach!

In your custom table model (just extend default table model if you are new to tables), you need to override the following methods

1) getColumnCount() Ideally these will be the fields in your Book object, which you want to display on the table
2) getColumnName()
3) getRowCount() Ideally this will return the size of your ArrayList. Typical pitfall area for a NPE! You have been warned!
4) getValueAt() This returns the values for the individual cells.
5) getColumnClass() Override this if you are interested in special renderers. By default, it returns Object.class

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic