• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Table cell renderer called, but not when set on column

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic