问题描述
我在 JTable
中以我想要的格式显示 Date
时遇到问题。我的 JTable
已使用ResultSet和列表创建。
I am having trouble displaying Date
s in the format I want in my JTable
. My JTable
has been created using a ResultSet and lists.
我在 getValueAt(。)
中尝试了以下操作,但没有运气:
I tried the following in getValueAt(.)
but no luck:
if(value instanceof Date)
{
//System.out.println("isDate");
DateFormat formatter = DateFormat.getDateInstance();
SimpleDateFormat f = new SimpleDateFormat("MM/dd/yy");
value = f.format(value);
Date parsed = (Date) value;
try {
parsed = (Date) f.parse(value.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
value = parsed.toString();
}
println(。)
永远不会打印,所以它甚至没有达到那个目的。显示的格式是 1992年4月10日
但我要 04/10/92
The println(.)
is never printed so it isn't even getting to that. The Format that is being displayed is Apr 10, 1992
but I want 04/10/92
当我们在 JTables
中讨论日期
的主题时......我将 isCellEditable(。)
视为true,但我无法编辑Date单元格。你是怎么做到的?
While we are on the topic of Date
in JTables
... I have isCellEditable(.)
as true but I cannot edit the Date cells. How do you do this?
推荐答案
不要覆盖 getValue
,使用而不是:
Do not override getValue
, use a TableCellRenderer
instead:
TableCellRenderer tableCellRenderer = new DefaultTableCellRenderer() {
SimpleDateFormat f = new SimpleDateFormat("MM/dd/yy");
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if( value instanceof Date) {
value = f.format(value);
}
return super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
};
table.getColumnModel().getColumn(0).setCellRenderer(tableCellRenderer);
这篇关于Jtable / ResultSet中的格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!