重写getColumnClass对于日期列不起作用 [英] Override getColumnClass not working for Date Columns

查看:107
本文介绍了重写getColumnClass对于日期列不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Matlab中使用JIDE网格排序和自动过滤功能.我已经覆盖了getColumnClass,并且过滤和排序对于整数,双精度和字符串列(分别对数字进行数字排序和对字符串进行按词法排序)工作都很好.

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

但是,我在日期"列中遇到了主要问题.我重写了getColumn类,并将其定义为Date.class.但是我认为我必须定义将日期(如原始数据中)传递给过滤和排序"的格式,以便它了解格式并正常工作.

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.

我看到JIDE自动筛选中的默认日期格式为'2016年4月7日'.我尝试将数据转换为相同的格式,但是没有运气.如果我尝试过滤日期,则会引发(未知源)异常.我认为它不理解我的日期格式.覆盖日期列的类时如何定义日期格式?

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)

这是我的TableModel类,它重写DefaultTableModel.

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;
        }
    }
}

推荐答案

我对JIDE一无所知,所以我的所有评论都是针对JDK中的常规类的.

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

我看到JIDE自动筛选中的默认日期格式为'2016年4月7日'.

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

在我看来,这就像一个弦乐.如果希望该列包含Date,则需要在TableModel中存储一个Date对象,而不是日期的String表示形式.

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.

例如:

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);
    }
}

您还可以查看表格格式渲染器,其中包含可重用的渲染器,您只需将Format对象提供给渲染器即可使用.这将节省您为所需的每种数据格式创建唯一的渲染器.

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.

我想我必须使用某种FormatConverter来做到这一点

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

您可以使用SimpleDateFormat类和parse(String)方法将String解析为Date对象.

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

这篇关于重写getColumnClass对于日期列不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆