I'm having an issue inserting images into a JTable. Ive searched and found there are 2 ways. You can either override the getcolumnclass of the tablemodel or create your custom tablecellrender.
I chose to make my own cell renderer with this code:
public class MyRenderer extends DefaultTableCellRenderer {
/*
* @see TableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
ImageIcon icon = new ImageIcon("Images/green.png");
setText((String)value);
setIcon(icon);
return this;
}
}
In the method where I fill in my table's fields, I am adding the images with this line..
laneTable.getColumnModel().getColumn(4).setCellRenderer(new MyRenderer());
The problem is that the images are showing up as blank white space. Its definitely rendering something because when you select the row, all of the fields highlight blue except for the 4th column(the one with the images) is still white. I cannot for the life of me figure out why the images are not showing. I've adjusted the row height in all sorts of ways to see if maybe the image was there, but out of view or something. Doesn't seem to be the case.
I also tried to do the override method but I guess I didn't do the syntax correctly because netbeans was giving syntax errors, and I have yet to find a decent example on how to do it.
Thanks.
edit: I've attempted the easier way, overriding the class using the code below but the cells are still blank. And I have tested inserting images into labels and those work, so the path is correct.
@Override
public Class getColumnClass(int column)
{
if (column == 4)
{
return ImageIcon.class;
}
return Object.class;
// other code; default to Object.class
}