JAVA:将图像放入 jTable 单元格中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3222951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:51:29  来源:igfitidea点击:

JAVA: Put Image in jTable Cell

javaimageswingjtablecell

提问by RYN

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

我需要在 jTable 单元格之一中显示图像。
我是这样写的:

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;
    }
}

and then used it as this:

然后将其用作:

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

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

但这不起作用
我怎么能做到这一点?

Thanks

谢谢

回答by camickr

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:

JTable 已经为图像提供了默认渲染器。你只需要告诉表格每列包含什么类型的数据,它就会选择最好的渲染器:

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.

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

b) add an ImageIcon to the table model.

b) 将 ImageIcon 添加到表模型。

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

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

回答by user268396

Hmm: jTable1.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());perhaps?

嗯:jTable1.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());也许?

Here's the relevant extract of some quick test code I put together to quickly verify my guess. It displays icons from a folder (it assumes all files in a folder are icons, so you should test it with something like an XDG icon theme sub directory). Install table model first then add the cell renderer on the columns:

这是我整理的一些快速测试代码的相关摘录,以快速验证我的猜测。它显示文件夹中的图标(它假定文件夹中的所有文件都是图标,因此您应该使用类似 XDG 图标主题子目录的内容对其进行测试)。首先安装表格模型,然后在列上添加单元格渲染器:

class Renderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent (JTable table,
                                                    Object value,
                                                    boolean isSelected,
                                                    boolean hasFocus,
                                                    int row, int column) {
        if(isSelected) {
            this.setBackground(table.getSelectionBackground());
            this.setForeground(table.getSelectionForeground());
        }
        else {
            this.setBackground(table.getBackground());
            this.setForeground(table.getForeground());
        }
        if(column == 0) {
            this.setText(list[row]);
        }
        else {
            // edit as appropriate for your icon theme
            this.setIcon(new ImageIcon("/usr/share/icons/default.kde4/16x16/apps/"+list[row]));
        }
        return this;
    }

}
class Model extends DefaultTableModel {

    @Override
    public boolean isCellEditable (int row, int column) {
        return false;
    }

    @Override
    public Object getValueAt (int row, int column) {
        return list[row];
    }

    @Override
    public int getRowCount () {
        return list.length;
    }

    @Override
    public int getColumnCount () {
        return 2;
    }

    @Override
    public String getColumnName (int column) {
        return column == 0? "Name" : "Preview";
    }

    @Override
    public Class<?> getColumnClass (int columnIndex) {
        return String.class;
    }
}
// edit base directory as appropriate for your icon theme of choice
static String[] list=new File("/usr/share/icons/default.kde4/16x16/apps/").list();