DefaultTableCellRenderer getTableCellRendererComponent never gets called

Posted by Dean Schulze on Stack Overflow See other posts from Stack Overflow or by Dean Schulze
Published on 2012-03-23T05:23:22Z Indexed on 2012/03/23 5:30 UTC
Read the original article Hit count: 214

Filed under:

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?

© Stack Overflow or respective owner

Related posts about swing