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?


Recommended Answers

All 11 Replies

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

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

I change the ImageIcon for each row

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

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.