File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTable TableCellRenderer based on row, col value help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable TableCellRenderer based on row, col value help" Watch "JTable TableCellRenderer based on row, col value help" New topic

JTable TableCellRenderer based on row, col value help

Sean Bellinger

Joined: Oct 16, 2008
Posts: 1
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 might be extremely easy, actually. I just can't see it :[
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

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.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: JTable TableCellRenderer based on row, col value help
It's not a secret anymore!