File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to extend a TableModel to have data that is not displayed in a table.   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to extend a TableModel to have data that is not displayed in a table.   " Watch "How to extend a TableModel to have data that is not displayed in a table.   " New topic
Author

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

Jason Richard
Ranch Hand

Joined: Oct 05, 2007
Posts: 69
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

Joined: Oct 05, 2007
Posts: 69
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
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: How to extend a TableModel to have data that is not displayed in a table.