• Post Reply Bookmark Topic Watch Topic
  • New Topic

Image in JTable Cell

 
Luis Fernandes
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
can someone explain me how to set a image to a JTable cell
i�have the following code :


Running my code all thinks work fine, but in the place of showing image the first cell of table show mw the string "imagens/check_16.gif"

My question is : How can i solve this problem ? what modification in my code i need to do to show the image instead the string ?

Best regards in advance
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two ways.

The first is to simply let your TableModel tell the table that the class for the specific column is Icon.class:

If you use DefaultTableModel just create an anonymous subclass that only overrides this method.
This can be used because by default, JTables have customized cell renderers for the following classes (and their subclasses):
Number, Float, Double, Date, Icon, ImageIcon and Boolean (actually Float and Double behave the same, and Icon and ImageIcon behave the same).
For instance, Boolean will give you check boxes instead which is quite nice, and the numeric classes will right-align the values.



If you can't have your table model return Icon.class you'll have to create a custom TableCellRenderer.

Best thing to do is create a sub class of DefaultTableCellRenderer:



However, in both cases your icon will not be scaled to the row height, so make sure it fits or increase the row height.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!