Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to extend a TableModel to have data that is not displayed in a table.

 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm trying to create a JTable, where I have a few pieces of data that are not part of the display. For example, if I select row one, it might have file name, user id displayed in the table, but I also have a bunch of fields i can access from that Row of the model that aren't rendered.

I'm assuming my best best to to extend the DefaultTableModel or the AbstractTableModel, but I'm having a tough time finding exactly what I should do with that once i do.

To be clear, I want each row to represent an instance of an Object. Some of those Object's variables are visible columns, the other is data I want to have access to but not display ever (so I'm not just trying to hide columns).

As always, thanks for even reading this!
 
Jason Richard
Ranch Hand
Posts: 80
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've decided to go with the following (in the table constructor)



which seems to keep the index of the item (which i want), but doesn't show it in the table (which i also want).

So the question is, this is great for something like text, but if I start adding bigger objects could this be an issue?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Richard wrote:I'm assuming my best best to to extend the DefaultTableModel or the AbstractTableModel, but I'm having a tough time finding exactly what I should do with that once i do.

To be clear, I want each row to represent an instance of an Object. Some of those Object's variables are visible columns, the other is data I want to have access to but not display ever (so I'm not just trying to hide columns).


Sure, this is a perfectly normal way to do things. The bit you're missing about how to implement it is, you override the getValueAt() and setValueAt() methods to expose only the attributes you want to be visible. Like this (pseudo-code):



You can also override the getColumnClass() method so that the default renderers work better.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic