简体   繁体   English

JTable日期列排序冻结

[英]JTable date column sort freezes

I am working with a JTable that contains a few columns with different datatypes (int, string, date). 我正在使用一个JTable,它包含几个具有不同数据类型的列(int,string,date)。 When I run the app the data displays fine but if I use the column headers to sort the data it freezes on the columns that contain Date objects. 当我运行应用程序时数据显示正常,但如果我使用列标题对数据进行排序,它会冻结包含Date对象的列。 Below is the code. 下面是代码。 Columns 8, 9, & 10 are the ones causing the problem. 第8,9和10列是导致问题的原因。 How do I make it so the Date columns are sortable? 如何使Date列可以排序?

public void updateLogTable() {

    DefaultTableModel model = (DefaultTableModel) logTable.getModel();
    List<LogObject> lstLogObjects = new ArrayList<LogObject>();
    lstLogObjects = LogManager.getLog();
    for (int i = 0; i < lstLogObjects.size(); i++) {
        Object[] temp = new Object[13];

        temp[0] = Integer.parseInt(lstLogObjects .get(i).getLogID());
        temp[1] = lstLogObjects .get(i).getLogType();
        temp[2] = lstLogObjects .get(i).getYear();
        temp[3] = lstLogObjects .get(i).getQuarter();
        temp[4] = lstLogObjects .get(i).getOriginalID();
        temp[5] = lstLogObjects .get(i).getSubject();
        temp[6] = lstLogObjects .get(i).getAction();
        temp[7] = lstLogObjects .get(i).getRequester();
        temp[8] = lstLogObjects .get(i).getADate(); //Returns java.util.Date
        temp[9] = lstLogObjects .get(i).getCDate(); //Returns java.util.Date
        temp[10] = lstLogObjects .get(i).getSDate(); //Returns java.util.Date
        temp[11] = lstLogObjects .get(i).getRemarks();
        temp[12] = lstLogObjects .get(i).getField1();

        model.addRow(temp);

    }
    model.fireTableDataChanged();
 }

Did you override the getColumnClass(...) method of your TableModel to return the proper class? 您是否覆盖了TableModel的getColumnClass(...)方法以返回正确的类?

The table sort methods will then sort the column and treat it as a Date rather than invoke toString() on the Date object. 然后,表排序方法将对列进行排序并将其视为Date,而不是在Date对象上调用toString()。

If you need more help then post your SSCCE demonstrating the problem. 如果您需要更多帮助,请发布您的SSCCE以证明问题。

I would recommend using JXTable for anything less trivial than displaying two columns. 我建议使用JXTable进行比显示两列更简单的事情。 Basic intro is for example here . 基本介绍就是这里的例子。

Other option is to use Long as element in table and use column renderer which would format date: 其他选项是在表中使用Long作为元素并使用列格式化日期的列渲染器:

 temp[8] = lstLogObjects .get(i).getADate().getTime()

 table.getColumnModel().getColumn(8).setCellRenderer( new DefaultTableCellRenderer(){
    public Component getTableCellRendererComponent(JTable table, Object value,
                                        boolean isSelected, boolean hasFocus,
                                        int row, int column){
        Object value2 = value; 
        if(row>0 && column==8) //put your own condition here
             value2 = new Date((Long)value).toString(); //your own formatting here
        return super.getTableCellRendererComponent(table, value2,
                                          isSelected, hasFocus,
                                          row, column);
     }
  });
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM