如何让我的 JTable 显示每一列的特定内容?
我可以为多个列使用一个对象吗?我想要一列显示日期 (dd.mm.yyyy) 和另一列显示时间 (hh:mm) 但正如我现在所拥有的那样,它只显示整个日期对象的字符串表示形式。
public class AppointmentTableModel extends AbstractTableModel {
...
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return appointments.get(rowIndex).getByColumn(columnIndex);
}
}
public class Appointment {
...
public Object getByColumn(int columnIndex) {
switch (columnIndex) {
case 0: return date;
case 1: return date;
case 2: return sample;
case 3: return sample;
case 4: return history;
case 5: return comment;
}
return null;
}
}
最佳答案
您可以为两列设置特定的渲染器,一个将采用 Date
对象并将其格式化为日期字符串,另一个将采用 Date
对象并进行格式化它作为时间字符串。
参见 this tutorial有关日期呈现的示例。
关于java - 如何在 JTable 中设置特定的列设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2775712/