I am trying to use the DefaultTableCellRenderer to format a column in JTable. For example: if user enter:
20140914
It will be format as:
2014-09-14
My code are following:
//Setting data type for each class
//This is a method within the DefaultTableModel
public Class getColumnClass(int col)
{
if (col == 0)
return DateRenderer.class;
if (col == 1 || col == 2)
return String.class;
else
return Double.class;
}
//This is a separate class that I have problem with:
static class DateRenderer extends DefaultTableCellRenderer
{
//Using Decimal format to format dash
DecimalFormatSymbols fmtSymbols = new DecimalFormatSymbols();
fmtSymbols.setDecimalSeparator('-');
DecimalFormat fmt = new DecimalFormat("####.##.##");
public DateRenderer() {
super(); }
public void setValue(Object value)
{
setText (fmt.format(value));
}
}
There's two problem with the code. One is that I have problem with the line:
fmtSymbols.setDecimalSeparator('-');
Another is that it will make my whole column become un-editable (Other column can still be edited and saved).