I need to render a java.util.Date in a JTable. I've implemented a custom renderer that extends DefaultTableCellRenderer (below). I've set it as the renderer for the column, but the method getTableCellRendererComponent() never gets called.
This is a pretty common problem, but none of the solutions I've seen work.
public class DateCellRenderer extends DefaultTableCellRenderer {
String sdfStr = "yyyy-MM-dd HH:mm:ss.SSS";
SimpleDateFormat sdf = new SimpleDateFormat(sdfStr);
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value instanceof Date) {
this.setText(sdf.format((Date) value));
}
else
logger.info("class: " + value.getClass().getCanonicalName());
return this;
}
}
I've installed the custom renderer (and verified that it has been installed) like this:
DateCellRenderer dcr = new DateCellRenderer();
table.getColumnModel().getColumn(2).setCellRenderer(dcr);
I've also tried
table.setDefaultRenderer( java.util.Date.class, dcr );
Any idea why the renderer gets installed, but never called?