JTable 日期列排序冻结
我正在使用一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否重写了 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.
我建议使用 JXTable 来处理比显示两列更简单的事情。基本介绍例如此处。
其他选项是使用 Long 作为表中的元素并使用列渲染器来格式化日期:
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: