aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Table cell renderer called, but not when set on column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Table cell renderer called, but not when set on column" Watch "Table cell renderer called, but not when set on column" New topic
Author

Table cell renderer called, but not when set on column

B Atkins
Greenhorn

Joined: Jun 11, 2009
Posts: 11
I have a table cell renderer. When I set it using JTable.setDefaultRenderer(class, TableCellRenderer), it works fine. However, when I set it using TableColumn.setCellRenderer(TableCellRenderer) it never gets called. I have several columns that are String, and it would be nice to apply the renderer to just the column that needs it, rather than all String typed columns.

What am I missing?

Works:


Doesn't work:


Thanks!


Two wrongs don't make a right... but three lefts do.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
Probably because you are changing the structure of the TableModel after you assing the renderer to the TableColumn. Changing the structure of the TableModel cause the TableColumnModel and the related TableColumns to be recreated so you loose your assignment of the renderer to the column.

So the solution is to not change the structure of the model after the renderer has been assigned to the TableColumn.
B Atkins
Greenhorn

Joined: Jun 11, 2009
Posts: 11
Rob Camick wrote:Probably because you are changing the structure of the TableModel after you assing the renderer to the TableColumn. Changing the structure of the TableModel cause the TableColumnModel and the related TableColumns to be recreated so you loose your assignment of the renderer to the column.

So the solution is to not change the structure of the model after the renderer has been assigned to the TableColumn.


I didn't know that, and placing the setCellRenderer call elsewhere is working fine. Thanks!
Doru Chiulan
Greenhorn

Joined: Sep 18, 2012
Posts: 2
B Atkins wrote:
Rob Camick wrote:Probably because you are changing the structure of the TableModel after you assing the renderer to the TableColumn. Changing the structure of the TableModel cause the TableColumnModel and the related TableColumns to be recreated so you loose your assignment of the renderer to the column.

So the solution is to not change the structure of the model after the renderer has been assigned to the TableColumn.


I didn't know that, and placing the setCellRenderer call elsewhere is working fine. Thanks!


It's been a while but there's a better solution.

In your table model do something similar to this : (in my case first column was an imageicon, the others Strings)

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4529
    
    5

Doru Chiulan wrote:It's been a while but there's a better solution.

In your table model do something similar to this : (in my case first column was an imageicon, the others Strings)

I think you missed this in the first post:
B Atkins wrote:I have several columns that are String, and it would be nice to apply the renderer to just the column that needs it, rather than all String typed columns.


Also, when referring to Java classes, and particularly JDK classes, please use the correct capitalization. ImageIcon, not imageicon.

And welcome to the Ranch!


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Table cell renderer called, but not when set on column