Forums Register Login

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

+Pie Number of slices to send: Send
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!
1
+Pie Number of slices to send: Send
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?
1
+Pie Number of slices to send: Send
 

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.
You have to be odd to be #1 - Seuss. An odd little ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 740 times.
Similar Threads
dynamic width of 2 fields, 2nd field width = 1st field width
Wanted: conceptual tutorial on JTable
custom table model and column model for JTable
How to dispaly a row in a table condionally?
Table Rows
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:01:45.