动态添加图像到JTable单元格 [英] Dynamically add images to JTable cells

查看:92
本文介绍了动态添加图像到JTable单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码动态地向单元格添加数据:

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

推荐答案

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.

这篇简短的文章可以帮助您使用图像渲染器: http://mdsaputra.wordpress.com/2011/06/13/swing-hack-show-image-in-jtable/

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

这篇关于动态添加图像到JTable单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆