Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable TableCellRenderer based on row, col value help

 
Sean Bellinger
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying different things for awhile now only to end up at the same place. I've searched the interwebs and still can't find a good solution to my problem:

I have a jTable with 5 columns set up in the following way
0 1 2 3 4
| # | type | Table Name | Field Name | Value |

I have 2 types {Variable, SMDB} if variable is selected, col 2 (Table Name) is set to a JComboBox with the tables only Variable types can access. Once a table Name has been chosen from col 2; col 3 is set to a combo box with the fields only available from the selected table.

as you can see I can't implement a columnmodel type cell editor beucase the editor is row/col independent because there's mutable rows all having different type/table name/field name combinations.
I've been playing around with overriding getValueAt(int row, int col) to check the previous col and return the combobox but I can't seem to render it. The cell will just have a string representation of the object.
I've also tried custom cell renderers but I can't seem to get it right.

Can anyone help?

some info if needed:
tableNames are in a linked hashmap where the key is the tablename and the value is a hashset of fieldNames

this table is displayed once someone clicks on a "rule" from a JTree. The "rule" will have data to put into the JTable that will work with the previously stated combo boxes.
This data is editable -- the comboboxes come into play.
The user can add new rows -- the comboboxes are again used.

if anyone could offer some insight, links, code, etc. it would be awesome. I'm fairly certain this isn't impossible...it might be extremely easy, actually. I just can't see it :[
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can override "public TableCellRenderer getCellRenderer(int row, int column)" and "public TableCellEditor getCellEditor(int row, int column)" to return a custom cell renderer / editor based on the cell, not just the column. I used that to be able to display a different renderer per cell myself, like a check box for Booleans, images for Image / ImageIcon, etc. And all in the same column.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic