• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TableCellRenderer based on JComponent executing but nothing shows

 
Malcolm Storey
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a simple pie-chart class on the web. It works fine in normal use. I added implements TableCellRenderer and used it as a CellRenderer but the pie charts don't show in the cells. (For simplicity, the chart isn't yet connected to any data.)

It tried changing paint to paintComponent but it made no difference.

I've followed it through with the debugger: getTableCellRendererComponent is being executed for the right column in the JTable and paint is being called for each cell.

 
Darryl Burke
Bartender
Posts: 5131
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably just need to override getPreferredSize() to return an appropriate Dimension.
 
Malcolm Storey
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
override getPreferredSize()


Thanks for the suggestion, but it didn't fix it.
 
Malcolm Storey
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've overridden all three



The debugger shows that none of them are called.

 
Darryl Burke
Bartender
Posts: 5131
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is getTableCellRendererComponent(...) called?
 
Malcolm Storey
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is getTableCellRendererComponent(...) called?


Yes. Once, for that column, in each row (as expected)
 
Malcolm Storey
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Progress at last!

If I change

to

it works.

getBounds returned x=196, y=0, w=26, h=15

g.getClipBounds returns 0,0,26,15

This was more by trying things than understanding what was happening. Presumably different coord systems?
 
Darryl Burke
Bartender
Posts: 5131
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good for you that you fixed it, but you don't need either of getBounds() or g.getClipBounds(). Just use getWidth() and getHeight().

The Rectangle returned from getBounds() is always in the coordinate system of the parent Container.

Your custom drawing always needs a top left of [0, 0] in the coordinate system of the Component being painted. Which is always [0, 0] in the coordinate system of the passed in Graphics parameter.
 
Malcolm Storey
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Darryl.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic