• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JTable Troubles.

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just tried that, didn't have any effect.
 
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 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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then, how would I get the Icons to display? If I comment out the line that adds my custom renderer, nothing happens.
 
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
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See I have that method in my TableModel class. Why does it have to be in Table rather then TableModel?
 
Sheriff
Posts: 28362
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic