JTable 日期列排序冻结

发布于 2024-12-08 23:10:29 字数 1346 浏览 0 评论 0原文

我正在使用一个 JTable,其中包含一些具有不同数据类型(int、string、date)的列。当我运行应用程序时,数据显示正常,但如果我使用列标题对数据进行排序,它会冻结在包含日期对象的列上。下面是代码。第 8、9 列10 是导致问题的原因。如何使日期列可排序?

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

I am working with a JTable that contains a few columns with different datatypes (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. Below is the code. Columns 8, 9, & 10 are the ones causing the problem. How do I make it so the Date columns are sortable?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我恋#小黄人 2024-12-15 23:10:30

您是否重写了 TableModel 的 getColumnClass(...) 方法以返回正确的类?

然后,表排序方法将对列进行排序并将其视为 Date,而不是对 Date 对象调用 toString()。

如果您需要更多帮助,请发布您的 SSCCE 来演示问题。

Did you override the getColumnClass(...) method of your TableModel to return the proper class?

The table sort methods will then sort the column and treat it as a Date rather than invoke toString() on the Date object.

If you need more help then post your SSCCE demonstrating the problem.

莫多说 2024-12-15 23:10:30

我建议使用 JXTable 来处理比显示两列更简单的事情。基本介绍例如此处

其他选项是使用 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);
     }
  });
 }

I would recommend using JXTable for anything less trivial than displaying two columns. Basic intro is for example here.

Other option is to use Long as element in table and use column renderer which would format date:

 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);
     }
  });
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文