This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Image is not showing in jTable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image is not showing in jTable." Watch "Image is not showing in jTable." New topic
Author

Image is not showing in jTable.

Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

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


Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

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.


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

Joined: Sep 25, 2004
Posts: 156

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

Joined: Jan 09, 2008
Posts: 25
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

Joined: Jul 26, 2007
Posts: 10171
    
    8

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

Joined: Sep 20, 2005
Posts: 862
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 ]

bitguru blog
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

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

Joined: Sep 20, 2005
Posts: 862
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

Joined: Sep 25, 2004
Posts: 156

Can anybody reply me where i make changes in any of the three example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Image is not showing in jTable.
 
Similar Threads
Image Renderer for JTable
jtable
JTable Updating
Pasting image in JTable cells
JTable