Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maneesh,

I will let you know after reading the page.................
 
Rob Camick
Ranch Hand
Posts: 2582
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2582
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you like spending time reinventing the wheel I see.
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic