How to show ImageIcon inside cell of JTable
I'am trying to display an ImageIcon in a JTable column. The only problem
is that I'am not seeing the ImageIcon but a String value "com.sun.java.swing.ImageIcon@266b10".
I think this is the memory address of the Icon. I thought Boolean, Number, ImageIcon and Object
are four types of data supported by JTable. Isn't it?
Does anyone has same experience and can share solution with me?
Thanks,
Hong Hsu [email protected]
Re: How to show ImageIcon inside cell of JTable
It might be a problem with your CellRenderer. Because this value is nothing but the ImageIcon.toString() value. That means the CellRenderer has converted your ImageIcon object to a string and displaying it rather than displaying as a component. So check your CellRenderer.
regards,
arun...
Re: How to show ImageIcon inside cell of JTable
Just set the icon of your cell renderer (if you are using or extending from DefaultTableCellRenderer, then it is just a JLabel and you can set an icon in JLabel:
class ErrorMessageCellRenderer extends DefaultTableCellRenderer
{
public void setValue(Object value)
{
if (value != null)
{
if (value instanceof ErrorMessage)
{
ErrorMessage em = (ErrorMessage)value;
setText(em.message);
setIcon(em.icon);
}
else
{
setText(value.toString());
}
}
else
{
setText(" ");
}
}
}
Re: How to show ImageIcon inside cell of JTable
Just modifying three line code of getColumnClass() method solves the problem. Following code say all:
public Class getColumnClass(int c) {
if(c == 0)
return ImageIcon.class;
return Object.class;
}
Thanks for your response.