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.
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 ]
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...
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?
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: https://coderanch.com/t/342132/GUI/java/JTable-different-rows-set-different