Skip to main content

Formatting a date in a JXTable

No replies
stevensegers
Offline
Joined: 2006-07-26
Points: 0

Hello
I've recently started using a JXTable to visualize some date and highlighting some rows using highlighers. This works great.
However, I have one date column that I would like to format using a specific pattern. This should be a simple task but I've been searching for hours for a sollution but can't find any.
I've basically tried two approaches: using CellRenderers and using highlighters, but none seem to work:
Cellrenderer approach:
<code>
logTable.setDefaultRenderer(Date.class, new DefaultTableRenderer(new StringValue() {

public String getString(Object value) {
if (value == null) return null;
return DATE_FORMAT.format((Date) value);
}
}));
</code>
Highlighter approach:
<code>
logTable.addHighlighter(new AbstractHighlighter() {

@Override
protected Component doHighlight(Component renderer, ComponentAdapter componentAdapter) {
Object value = componentAdapter.getValue();
if (value != null && value instanceof Date) {
if (renderer instanceof JLabel) {
JLabel label = (JLabel) renderer;
label.setText(DATE_FORMAT.format((Date) value));
componentAdapter.
}
}
return renderer;
}
</code>
BTW My tablemodel is identifying the date collumn as beeing of type Date, but it seems the getColumnClass method on the model is never called.
I would really appreciate any pointers on how to implement this.
Best regards
Steven