File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable Troubles. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Troubles." Watch "JTable Troubles." New topic
Author

JTable Troubles.

Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
So, I'm essentially trying to setup my JTable to display only images. I tried searching around, found some examples, and followed to nearly verbatim. Problem is, I don't have any images displayed.
This is what my table looks like:



however, each image is simply a red square, so I should be seeing a sea of red, instead of the above. If I double click on a cell, I see the file path. But, the code I added should have fixed that.

Code for the Table:


Code for my TableModel:



and finally code for my CellRenderer:


My progam hits the "setValue" method (proven with a println), which just confuses me even more, as the setValue should be setting the ImageIcon.

And the forum post where I got most of my help (after reading the Java Tutorial)
http://www.velocityreviews.com/forums/t126359-jtable-and-renderers.html

Thanks for all the help.
kalle svensson
Greenhorn

Joined: Oct 05, 2009
Posts: 13
hi, i always get troubles with tables. im not sure if i understand . but have you tried to set the setOpaque method to true in the jtable and in the cellrenderer and the same with the repaint method. im not sure i understand but it helped me once when i rendererd images on a jlabel. sorry if i have missed out something.
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
Just tried that, didn't have any effect.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
There is no need for a custom renderer, JTable already has a default Icon renderer.

Your problem is that your custom renderer code is wrong. The setValue is used to set the value of the cell to be rendered. It does NOT update the renderer with any data. The getTableCellRenderer(...) method is then invoked and will take the value and update the renderer properties. Since you did not override this method the default behaviour is to simply use the toString() of the value, which is why you see the toString() of the ImageIcon which is just the file name.
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
Then, how would I get the Icons to display? If I comment out the line that adds my custom renderer, nothing happens.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
It should work. There are two steps:

1) add ImageIcon to the model
2) override the getColumnClass(...) method to return ImageIcon so the table know which renderer to choose.

You appear to have done both steps, so based on the random lines of code you posted I don't know what you are doing wrong.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.

Here is a simple working SSCCE:

Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
See I have that method in my TableModel class. Why does it have to be in Table rather then TableModel?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

But your code is different in that it returns ImageIcon.class. Perhaps Rob's code actually returns something else which happens to work better? Do some debugging to see.
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
Okay now I'm getting the image, along with a white space next to it. Double clicking on the cell will show the text of the filename and blank out the cell.

EDIT: Actually, nevermind, it's not an invisible text field, it's the column being too wide, and the image being centered. So, technically, my problem is solved, and I thank you for you help.

Now I just have to have the columns set their width correctly.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Double clicking on the cell will show the text of the filename and blank out the cell.


Make the cells non editable.

Now I just have to have the columns set their width correctly.


Table Column Adjuster
 
wood burning stoves
 
subject: JTable Troubles.