javaswingmatlabdatejide

Override getColumnClass not working for Date Columns


I am using JIDE grids Sorting and Autofiltering capability in Matlab. I have overridden the getColumnClass and filtering and sorting is working well for Integers, Double and String Columns (sorting numerically for numbers and lexically respectively for strings).

However, I am facing a major issues with Date columns. I have overridden getColumn class and defined as Date.class. But I think I have to define the format in which the dates (as in raw data) are being passed to Filtering and Sorting for it to understand the format and work properly.

I see default date format in JIDE Autofiltering is '07-Apr-2016'. I have tried converting my data to same format but no luck. If I try to filter the dates, it throws (Unknown Source) exception. I think it does not understand my date format. How can I define the date format when overriding the class for Date column?

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
                               java.util.Date cannot be cast to java.lang.String
       at java.lang.String.compareTo(Unknown Source)
       at com.jidesoft.filter.LessThanFilter.isValueFiltered(Unknown Source)
       at com.jidesoft.grid.FilterableTableModel.shouldBeFiltered(Unknown Source)

Here is my TableModel class that overrides DefaultTableModel.

import javax.swing.table.*;
import java.util.Date;

class MyTableModel extends DefaultTableModel {

    public MyTableModel(Object rowData[][], Object columnNames[]) {
        super(rowData, columnNames);
    }
    @Override
    public Class getColumnClass(int col) {
        switch (col){
            case 0:
                return Integer.class;
            case 1: case 2: case 9:
            case 10: case 33:
                return String.class;
            case 3:
                return Date.class;
            default:
                return Double.class;
        }
    }
    @Override
    public boolean isCellEditable(int row, int col) {
        switch (col){
            case 28: case 29: case 30: case 31: case 32:
                return true;
            default:
                return false;
        }
    }
}

Solution

  • I don't know anything about JIDE so all my comments are for regular classes in the JDK.

    I see default date format in JIDE Autofiltering is '07-Apr-2016'.

    That looks like a String to me. If you want the column to contain a Date, then you need to store a Date object in the TableModel, not a String representation of a date.

    Then you would typically add a custom renderer to the table to display the date in an appropriate format.

    For example:

    public class YMDRenderer extends DefaultTableCellRenderer
    {
        private Format formatter = new SimpleDateFormat("yy/MM/dd");
    
        public void setValue(Object value)
        {
            //  Format the Object before setting its value in the renderer
    
            try
            {
                if (value != null)
                    value = formatter.format(value);
            }
            catch(IllegalArgumentException e) {}
    
            super.setValue(value);
        }
    }
    

    You can also check out Table Format Renderers which contains reusable renderers you can use simply by providing a Format object to the renderer. This will save you creating unique renderers for each data format you want.

    Edit:

    I guess I have to use FormatConverter of some sort to do that

    You can use the SimpleDateFormat class and the parse(String) method to parse the String to a Date object.