Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image is not showing in jTable.

 
Anand Karia
Ranch Hand
Posts: 156
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody please tell me where i am wrong. Image on jTable is not displaying.

3 Button and 3 methods are called on button click event.



THE RESULT ON JTABLE IS "javax.swing.ImageIcon@b89838"






IMAGE SHOWN ON LABEL BUT ON TABLE IT SHOWS "javax.swing.ImageIcon@112f614"




HERE RESULT SHOWN ON JTABLE =
BufferedImage@1d9dc39: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@93dcd transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 166 height = 202 #numDataElements 3 dataOff[0] = 2
 
Maneesh Godbole
Saloon Keeper
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The table queries the table model regarding the object class and then picks up the appropriate renderer.
The DefaultTableModel returns Object.class as the default value for getColumnClass(int columnCount). For Object.class the table calls the toString() and displays the value.

You need to return Image.class for the column which should displays images.
Easiest way to do this is extend the DefaultTableModel and override this method.
 
Anand Karia
Ranch Hand
Posts: 156
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Maneesh Godbole

Thank you very much for replying me but i want to let you know that i have done the same but could not get the result. Can you please send me code/example?
 
raja ramesh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,i think this is not the right way to add an image icon to JTable directly,instead you use TableCellRenderer ..u will get the answer ..
 
Maneesh Godbole
Saloon Keeper
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anand Karia:
.. i have done the same but could not get the result. Can you please send me code/example?


Better still, why dont you post your table model code and we can figure out whats wrong? Its always better to try out things by yourself.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by raja ramesh:
i think this is not the right way to add an image icon to JTable directly,instead you use TableCellRenderer


It is certainly possible to write a TableCellRenderer to show
icons, but I disagree that he's not doing it "the right way."

It looks like the icon is fine in the table model, just that
(as Mr. Godbole mentions) he needs to return Icon.class in
his table model's getColumnClass() method. It's that simple.

[edit: Mr. Godbole actually mentions Image.class, while
in this post I recommend Icon.class. They are different.]
[ January 16, 2008: Message edited by: Brian Cole ]
 
Maneesh Godbole
Saloon Keeper
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by raja ramesh:
hi ,i think this is not the right way to add an image icon to JTable directly,instead you use TableCellRenderer ..u will get the answer ..


You still need to define the Class for which the renderer needs to be used. So if you define the renderer for Image.class, the table model still returns the Object.class and so the renderer wont be picked up.
So the model definitely needs to be tweaked to return the proper class.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:
You still need to define the Class for which the renderer needs to be used. So if you define the renderer for Image.class, the table model still returns the Object.class and so the renderer wont be picked up.
So the model definitely needs to be tweaked to return the proper class.


You are correct about the model needing to return the proper class.

I recommend that you have your model return Icon.class or ImageIcon.class, not Image.class. That way you don't have to define a renderer, as JTable automatically provides one for you.

Of course, the table model must actually return instances of Icon or ImageIcon for the column. Looking at the original post in this thread, ImageIcons are being returned with show2() and show3(), so no problem there.
[ January 16, 2008: Message edited by: Brian Cole ]
 
Anand Karia
Ranch Hand
Posts: 156
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody reply me where i make changes in any of the three example.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic