Java: put the image into JTable cell

I need to display the image in a JTable cell

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

Then use it as:

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

But it doesn't work. How can I do that?

thank you

Solution

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

a) Override the getcolumnclass () method of JTable or tablemodel to return the data class in the column In this case, you should return an icon class.

b) Add imageicon to the table model

JTable will now use the default icon renderer for that column

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>