954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ImageIcon In jTable cell in netbeans

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

snigger
Newbie Poster
6 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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!

snigger
Newbie Poster
6 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
musthafa.aj
Posting Whiz
312 posts since Nov 2009
Reputation Points: 19
Solved Threads: 45
 

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?

snigger
Newbie Poster
6 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Does it work?

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

yes it works

snigger
Newbie Poster
6 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

No
I change the ImageIcon for each row

snigger
Newbie Poster
6 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: