batsta13
batsta13

Reputation: 539

Dynamically add images to JTable cells

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

Upvotes: 0

Views: 6876

Answers (2)

Waddas
Waddas

Reputation: 1402

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/

Upvotes: 1

Amarnath
Amarnath

Reputation: 8865

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.

Upvotes: 1

Related Questions