File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Images in a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Images in a JTable" Watch "Images in a JTable" New topic
Author

Images in a JTable

Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
Hello,

I would like a column in my JTable to be one that displays one of two images. If the database returns true, it will display image 1 (a tick). If the database returns false, it will display image 2 (a cross). I know that by default the JTable returns a JLabel. Despite having the correct image icon in the [][]array that populates the JTable, when the table is run it simply returns the location of the image. This is to be expected. However, how can i change the JTable so it returns an ImageIcon and not its default JLabel? I am figuring the table renderer will come into play. I am already using a Table renderer like below. All that the renderer does at the minute is return different coloured rows. Anyone got any ideas how i can solve my problem? In case its significant, only column four of the JTable needs to return a ImageIcon, the other columns should remain as the default JLabel.




Any help would greatly be appreciated. Thanks
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
It's not so much in how you define the cell renderer but in how you associate a renderer with a column. You can either explicitly associate a cell renderer with a TableColumn from the TableColumnModel, or change the default renderer for Boolean.class. Here is an example of the latter.
(Rather than defining new renderers and editors, I just change the icon used by them. Try clicking on the second column!)
[ January 17, 2006: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I should add that before you supply your own icons, have you thought about using the default?
Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
Thanks Jeff for all your help and code. Has worked fine. All i needed to do was add:

public Class getColumnClass(int columnIndex)
{
return columnIndex == 3 ? ImageIcon.class : Object.class;
}

to my table model and it works a treat.

Thanks again!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Marcus Hathaway:
return columnIndex == 3 ? ImageIcon.class : Object.class;

That works, but eventually you may want to rethink your TableModel. The TableModel *is* the data, and ImageIcon isn't really data -- I thought the data was a boolean., so an ImageIcon is more related to rendering the data. Where this could get you in trouble is if you want to programmatically access data in the table. Then you would have to reinterpret an ImageIcon as a true/false value...
Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
Hi Jeff,

Yes your right. However, in this particular instance, its not important for me to treat the image as anything other than that. But i totally take on board what you are saying, and in the example that you provided - doing it the 'simple' way like i have would not be possible.

I'm not sure if i may have to change my table model as you suggest anyway. As the code in my first post states, i am using a cell renderer to make the rows different colours. However, now that i have added the new code to my table model, the column which displays the image does simply that....it no longer has the row colour. So in columns 1, 2, 3, the row is fine with a nice blue or white background colour. However, column 4 simply displays the image and is no longer affected by the individual row colour. Anybody have any suggestions to how i can affect the row colour for column 4 again? Do i need to somehow register column 4 with the cell renderer again?

Again, any advice would be greatly appreciated.

Cheers!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
One common effect in a JTable is to have different background colors for rows. Apart from indicating selection, you many want the rows to alternate colors: white, light-gray, white, light-gray,... or have the color indicate some sort of priority (green, yellow, red) etc...

The easiest wat to do this is not to tinker with the renderers but to override JTable's prepareRender method. The reason this is preferred is because it cuts across all renderers: you often have several in a table: for text, numbers, currency, dates, booleans, images, etc... and you wouldn't want to repeat code in all those renders. There is an example of overriding prepareRender in this thread: http://www.coderanch.com/t/342132/GUI/java/JTable-different-rows-set-different
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Images in a JTable
 
Similar Threads
Color in JTable
Table Row Selection problem while adding any Image in a JTable
JTable
Is there any way to insert a JLabel(with an image icon) in a JTable cell?
Jtable background image