File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Background color of ImageIcon in a JTable

 
Henrik Engert
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTable with a column of ImageIcons, but I want to be able to set the background color of the cell where the ImageIcon resides.

Is this possible?

I can do it with JCheckBox's etc. don't know how to do it with ImageIcons.

It seems like I have to override the paint method in ImageIcon maybe???

Thanks
 
Brian Cole
Author
Ranch Hand
Posts: 886
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you could create your own renderer for icons, just like last time.

Actually, I think the default renderer for Icons does use either table.getBackground() or table.getSelectionBackground() for its background, so calling table.getBackground() might work if it wouldn't have any other negative effects.

It seems like I have to override the paint method in ImageIcon maybe???


Overriding your Icon's paint() method won't do anything (except perhaps for the color under the transparent parts of your Icon, if it has any) because that's not where the cell area outside of the Icon gets painted.

Well actually, the Graphics object passed to Icon.paint() usually isn't clipped, so it is generally possible to paint outside the Icon's region from that method. But doing so would be a hack. Better, I think, to handle the cell background in your table cell renderers.
 
Henrik Engert
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

What threw me of was that ImageIcon does not have a background method, but it kind of makes sence to call the table's method.

I'll give it a shot.

/Henrik
 
Henrik Engert
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The best way was to make a class that extended DefaultTableCellRenderer.

In that class override "getTableCellRendererComponent".

Then use a JLabel and embedd an ImageIcon inside the JLabel.

This worked perfectly.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic