Question

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).

Was it helpful?

Solution 2

"well, this is only a simple program that design to handle yyyyMMdd at the moment. I want to take one step at a time and to understand how to program and format the date"

  • As for the format, you shouldn't be using a DecimalFormat. Instead use a DateFormat. A commonly used subclass is SimpleDateFormat

  • As for your current implementation of DefautTableCellRenderer, you are missing a key component, which is the overriding of getTableCellRendererComponent. You can see How to use Renders for a proper way. Also there are may questions you can look at.

  • As for your overriding of getColumnClass(), the column class shoun't be the renderer component. You couln't instead make the column class Date and have the default renderer for Date do the rendering for you. You can see more at Using Renderers. If you don't want to it be Date and keep it as String, then the renderer should handle the re-rendering to the correct format.

  • As for other options to handle the entering of invalid input:

    • You can look into using an InputVerifier for the editing component for the cell. You can see an example here and also many questions.
    • You may even want to look into using some kind of date picker component for the editor of the cell. You can see many related questions here
    • You may want to use a JSpinner for the editor. See many questions

OTHER TIPS

Don't use a DecimalFormat to render the Date. Instead you should be using a SimpleDataFormat. See Table Format Renderers for a renderer that you can use.

Another is that it will make my whole column become un-editable

A renderer does NOT control if a column is editable or not. The is the job of the isCellEditable(...) method.

Also, your getColumnClass(...) implementation is wrong. You should not be returning DateRender.class. Instead you should be returning the class of the data stored in the column, which should probably be Date.class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top