aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Background color of ImageIcon in a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Background color of ImageIcon in a JTable" Watch "Background color of ImageIcon in a JTable" New topic
Author

Background color of ImageIcon in a JTable

Henrik Engert
Ranch Hand

Joined: Apr 26, 2005
Posts: 70
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


SCJP 5.0, SCWCD
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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.


bitguru blog
Henrik Engert
Ranch Hand

Joined: Apr 26, 2005
Posts: 70
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

Joined: Apr 26, 2005
Posts: 70
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
 
subject: Background color of ImageIcon in a JTable