Hi
I need to show an image thumbnail in one of jTable cells
But I don't know how to do it!
I'm using netbeans to design GUI .
How can I do that?

Thanks

Recommended Answers

All 11 Replies

Hi
I did it as this:

class mcr extends DefaultTableCellRenderer{
        public Class getColumnClass(int c) {
            return ImageIcon.class;
        }
    }

and used like this:

jTable1.getColumn("image").setCellRenderer(new mcr());
        DefaultTableModel Tmodel=(DefaultTableModel) jTable1.getModel();
        jTable1.setRowHeight(80);
        ImageIcon icon = new ImageIcon("/home/ariyan/Desktop/71290452.jpg");
        Tmodel.setValueAt(icon, 0, 3);

But it just shows toString of ImageIcon and not the image!

getColumnClass is part of TableModel not TableCellRenderer. If you mean to override methods then use the @Override annotation that things like that don't happen. See the examples/tutorials that that Google search throws up.

Hi
I did as 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.setIcon((ImageIcon)value);
        return lbl;
      }
    }

and used like this:

ImageIcon icon = new ImageIcon("/home/ariyan/Desktop/pic/4vn2hdi.jpg");
        jTable1.getColumnModel().getColumn(3).setCellRenderer(new ImageRenderer());
        DefaultTableModel Tmodel=(DefaultTableModel) jTable1.getModel();
        jTable1.setRowHeight(150);
        Tmodel.setValueAt(icon, 0, 3);

Is this correct?
Or needs change?

Does it work?

yes it works

And doesn't, say, display the same Icon in every row in that column?

No
I change the ImageIcon for each row

It doesn't works for difefrent images for rows.Please help!

It all depends on whether you set the custom renderer for a column, a row, or an individual cell. The code above sets it for a column. ie all the cells in that column.
Oracle's tutorial explains it all with examples.
https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer
(about 1/2 way down the page, starting with "To specify a cell-specific renderer,")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.