1

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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Is the data being entered by the use from the actual table, or from like a text field or something? – Paul Samsotha May 13 '14 at 02:12
  • http://i.stack.imgur.com/KRVMA.png This is an image of my GUI interface. What I want is that: if the user enter the date under the date column, it will automatically adding '-' (dash) between the numbers. Thanks for helping me. – user2789240 May 13 '14 at 02:55
  • What happens when the user enters something besides the format `yyyyMMdd`? How will you handle _that_? – Paul Samsotha May 13 '14 at 02:59
  • 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 before I move on into the next step(handling the "error" part of user entering the wrong number). – user2789240 May 13 '14 at 03:21

2 Answers2

3

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
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
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720