简体   繁体   English

JAVA:将图像放入jTable Cell

[英]JAVA: Put Image in jTable Cell

I need to display an image in one of jTable cells. 我需要在一个jTable单元格中显示图像。
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;
    }
}

and then used it as this: 然后用它作为:

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

But this didn't work 但这没效果
How I can do that? 我怎么能这样做?

Thanks 谢谢

JTable already provides a default renderer for images. JTable已经为图像提供了默认渲染器。 You just need to tell the table what type of data is contained in each column and it will choose the best renderer: 您只需要告诉表格每列中包含哪种类型的数据,它将选择最佳渲染器:

a) override the getColumnClass() method of the JTable or the TableModel to return the class of data in the column. a)覆盖JTable的getColumnClass()方法或TableModel以返回列中的数据类。 In this case you should return an Icon.class. 在这种情况下,您应该返回一个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将使用该列的默认Icon渲染器。

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

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). 它显示文件夹中的图标(它假定文件夹中的所有文件都是图标,因此您应该使用类似XDG图标主题子目录的方法进行测试)。 Install table model first then add the cell renderer on the columns: 首先安装表模型,然后在列上添加单元格渲染器:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM