根据您之前的问题,过程几乎相同,只是您想使用渲染器而不是编辑器
public class DateCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable jtable, Object value, boolean selected, boolean hasFocus, int row, int column) {
if (value instanceof Date) {
// You could use SimpleDateFormatter instead
value = DateFormat.getDateInstance().format(value);
}
return super.getTableCellRendererComponent(jtable, value, selected, hasFocus, row, column);
}
然后,要应用渲染,您可以将其应用到特定列(因此只有该列将使用它),或者在Date
您可能希望使用该Date
值的所有列的情况下使用它...
JTable table = new JTable();
DateCellRenderer renderer = new DateCellRenderer();
// Apply for a single column
table.getColumnModel().getColumn(0).setCellRenderer(renderer);
// OR apply for all columns using the Date class
table.setDefaultRenderer(Date.class, renderer);