0

I am dynamically adding data to a cell with the following code:

for(int i = 0; i < matchedSlots.size(); i++)
{  
  String title = matchedSlots.get(i).getTitle();
   String director = matchedSlots.get(i).getDirector();
   int rating = matchedSlots.get(i).getRating();
   int runTime = matchedSlots.get(i).getRunningTime();

    DefaultTableModel tm = (DefaultTableModel) searchResults.getModel();    
    tm.addRow(new Object[] {title,director,rating,runTime});
 }

what do I need to add to the above to be able to add an image in the first cell of each row

batsta13
  • 549
  • 2
  • 12
  • 26

2 Answers2

1
ImageIcon image = new ImageIcon("image.gif");
...
tm.addRow(new Object[] {image,title,director,rating,runTime});

You may need to change your table model to account for the new column if you haven't already.

This short article should help you with the image renderer: http://mdsaputra.wordpress.com/2011/06/13/swing-hack-show-image-in-jtable/

Waddas
  • 1,353
  • 2
  • 16
  • 28
  • I tried this and all it does is display the pathname. Rather than harding coding the pathname i used matchedSlot.get(i).getImagePath – batsta13 May 30 '13 at 13:05
1

By default JTable can render Images. You just need to override getColumnClass() in the TableModel and return Icon.class for 1st column.

Look at Renderers and Editors for more details.

Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • Look at the [answer](http://stackoverflow.com/questions/4941372/how-to-insert-image-into-jtable-cell) provided by @camickr – Amarnath May 30 '13 at 14:15