例如
private static class DateRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
if (!(value instanceof Date)) {
return this;
}
setText(DATE_FORMAT.format((Date) value));
return this;
}
}
编辑
import java.awt.GridLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class SimpleTableDemo extends JPanel {
private static final long serialVersionUID = 1L;
private boolean DEBUG = false;
private JTable table;
private JButton okButton;
private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian", "Date"};
private Object[][] data = {{"Kathy", "Smith", "Snowboarding", new Integer(5), (false), new Date()},
{"John", "Doe", "Rowing", new Integer(3), (true), new Date()},
{"Sue", "Black", "Knitting", new Integer(2), (false), null},
{"Jane", "White", "Speed reading", new Integer(20), (true), new Date()},
{"Joe", "Brown", "Pool", new Integer(10), (false), new Date()},};
private TableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
@Override
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
public SimpleTableDemo() {
super(new GridLayout(1, 0));
table = new JTable(model);
table.setRowHeight(20);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setFillsViewportHeight(true);
TableColumnModel m = table.getColumnModel();
m.getColumn(5).setCellRenderer(new SubstDateRenderer());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
class SubstDateRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
private Date dateValue;
private SimpleDateFormat sdfNewValue = new SimpleDateFormat("dd.MMMM.yyyy");
private String sdfNewValueString = "";
public SubstDateRenderer() {// formating TableCell
super();
setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
}
@Override
public void setValue(Object value) {
if ((value != null) && (value instanceof Date)) {
dateValue = (Date) value;
sdfNewValueString = sdfNewValue.format(dateValue);
value = sdfNewValueString;
}
super.setValue(value);
}
}
}