The problem lies with your TableModel, you have to tell the table that it has to render an image in that column overriding the getColumnClass(int column)
method of the model.
Look at this answer by camickr.
UPDATE
Minimal example of a JTable with an ImageIcon using DefaultTableModel's renderer to paint it. I borrowed the updateRowHeights()
code from this answer again by camickr, without it the icon was bigger than the row and wasn't fully displayed.
The important thing here is that now when the renderer calls getColumnClass(1)
, it gets ImageIcon.class
so the code to render icons will be executed. By default this method would return Object.class
and the renderer would ignore the fact that it's an icon.
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class ImageIconTable
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ImageIconTable().initGUI();
}
});
}
public void initGUI()
{
JFrame frame = new JFrame();
DefaultTableModel tableModel = new DefaultTableModel()
{
@Override
public Class getColumnClass(int column)
{
if (column == 1) return ImageIcon.class;
return Object.class;
}
};
tableModel.addColumn("Row 1");
tableModel.addColumn("Icons Row");
tableModel.addRow(new Object[]{"This cell is an Object", new ImageIcon("icon.jpg")});
_table = new JTable(tableModel);
updateRowHeights();
frame.add(new JScrollPane(_table), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private void updateRowHeights()
{
try
{
for (int row = 0; row < _table.getRowCount(); row++)
{
int rowHeight = _table.getRowHeight();
for (int column = 0; column < _table.getColumnCount(); column++)
{
Component comp = _table.prepareRenderer(_table.getCellRenderer(row, column), row, column);
rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
}
_table.setRowHeight(row, rowHeight);
}
}
catch(ClassCastException e) {}
}
private JTable _table;
}
It looks like this: