File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Table cell renderer called, but not when set on column

 
B Atkins
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rob Camick
Ranch Hand
Posts: 2476
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5116
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic