aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Is there any way to insert a JLabel(with an image icon) in a JTable cell? 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 "Is there any way to insert a JLabel(with an image icon) in a JTable cell?" Watch "Is there any way to insert a JLabel(with an image icon) in a JTable cell?" New topic
Author

Is there any way to insert a JLabel(with an image icon) in a JTable cell?

Renjith mohanan
Ranch Hand

Joined: Jun 19, 2009
Posts: 52
Hi All,
I need to set a JLabel in a JTable cell, is there any way to do this.

Actually what I need is, "I need to place a picture in table cell, so I thought that I need to set the picture as an image icon to a JLabel and then set the JLabel to the cell.

Is there any way to do this?

Any help is appreciable.

Thanks

Renjith


Personal E-mail: kalliyoorrenjith@gmail.com
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Check out the renderers section


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Renjith mohanan
Ranch Hand

Joined: Jun 19, 2009
Posts: 52
Thanks Maneesh,

I will let you know after reading the page.................
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
There is no need to create a custom renderer, although you do need to understand how renderers work and how the appropriate renderer is choosen.

JTable already suppost Icons. Just add the Icon to the model and override the getColumnClass method to tell the table the column contains Icons and the appropriate renderer will be choosen.

Renjith mohanan
Ranch Hand

Joined: Jun 19, 2009
Posts: 52
Thanks All,
I used the code, that is I extends JLabel implements TableCellRenderer



And then I set the corresponding column to the above cellrenderer as


Thanks

Renjith M
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
So you like spending time reinventing the wheel I see.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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
table.getColumnModel().getColumn(1).setCellRenderer(new JLabelCellenderer("path");


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 value is 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
  • setBackground(new Color(184,207,229))
  • each time in getTableCellRendererComponent(). Instead do something like
  • setBackground(mySelectedColor)
  • 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.)


    bitguru blog
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Is there any way to insert a JLabel(with an image icon) in a JTable cell?