• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Check out the renderers section
 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maneesh,

I will let you know after reading the page.................
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you like spending time reinventing the wheel I see.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
     
    I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic