This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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.
Joined: Mar 07, 2004
Then, how would I get the Icons to display? If I comment out the line that adds my custom renderer, nothing happens.
Joined: Jun 13, 2009
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.