T.V.M Renjith wrote:
I used the code, that is I extends JLabel implements TableCellRenderer And then I set the corresponding column to the above cellrenderer as
This renderer does not put an icon in a particular cell (as the thread title
might suggest) but shows the same icon for an entire column. So in your
example the image at "path" will be rendered in each row for column #1.
If that's what you want, then more power to you.
Generally, if you're going to write your own renderer, you would have the
getTableCellRendererComponent() method obtain the image from the value parameter somehow. (Perhaps valueis an Image or Icon already, or else
you look it up in a hashtable or something.) The point is, the image that
is set will depend on the value, so each row can show a different image.
Of course, it's probably easiest to not mess with renderers at all and use the
built-in JTable renderers. Simply have your table model return an Icon for the
cell's value and make sure getColumnClass() returns Icon.class for that column.
Another quibble with your renderer: You probably don't want to call
each time in getTableCellRendererComponent(). Instead do something like
where mySelectedColor is defined outside the method. That could save a
bunch of object instantiation. (This would be a bigger issue if you did this
for all cells, instead of just selected ones, but I thought I'd mention it.)