• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic