JAVA:将图像放入jTable Cell [英] JAVA: Put Image in jTable Cell

查看:149
本文介绍了JAVA:将图像放入jTable Cell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在jTable单元格中显示一个图像。

我写了这个:

I need to display an image in one of jTable cells.
I wrote this:

class ImageRenderer extends DefaultTableCellRenderer {
    JLabel lbl = new JLabel();

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int column) {
        lbl.setText((String) value);
        lbl.setIcon(new ImageIcon("/home/ariyan/Desktop/71290452.jpg"));
        return lbl;
    }
}

然后将其用作:

    jTable1.getColumn(0).setCellRenderer(new ImageRenderer());

但是这不起作用

我怎么能这样做?

But this didn't work
How I can do that?

谢谢

推荐答案

JTable已经为图像提供了默认渲染器。您只需要告诉表每列中包含哪种类型的数据,它将选择最佳渲染器:

JTable already provides a default renderer for images. You just need to tell the table what type of data is contained in each column and it will choose the best renderer:

a)覆盖JTable的getColumnClass()方法或TableModel返回列中的数据类。在这种情况下,你应该返回一个Icon.class。

a) override the getColumnClass() method of the JTable or the TableModel to return the class of data in the column. In this case you should return an Icon.class.

b)将一个ImageIcon添加到表模型中。

b) add an ImageIcon to the table model.

现在JTable将使用该列的默认Icon渲染器。

Now the JTable will use the default Icon renderer for that column.

这篇关于JAVA:将图像放入jTable Cell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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