java如何将CellRenderer用于GregoriaCalendar?
所以我一直在尝试使用教程中的示例,并对其进行更改,使其适合我的程序。 getColumnValue方法返回包含应该显示的信息的对象。这是一种方法,还是应该返回要显示的实际字符串。我想不是因为那样我会把演示文稿和数据混在一起,这是我试图避免的
public class IssueTableFormat implements TableFormat<Appointment> {
public int getColumnCount() {
return 6;
}
public String getColumnName(int column) {
if(column == 0) return "Datum";
else if(column == 1) return "Uhrzeit";
else if(column == 2) return "Nummer";
else if(column == 3) return "Name";
else if(column == 4) return "letzte Aktion";
else if(column == 5) return "Kommentar";
throw new IllegalStateException();
}
public Object getColumnValue(Appointment issue, int column) {
if(column == 0) return issue.getDate();
else if(column == 1) return issue.getDate();
else if(column == 2) return issue.getSample();
else if(column == 3) return issue.getSample();
else if(column == 4) return issue.getHistory();
else if(column == 5) return issue.getComment();
throw new IllegalStateException();
}
}
列0和1包含GregorianCalendar对象,但我希望列0显示日期,列1显示时间
所以我知道在这里使用CellRenderer会有所帮助
这就是我试过的
public class DateRenderer extends DefaultTableCellRenderer {
public DateRenderer() { super(); }
public void setValue(Object value) {
GregorianCalendar g =(GregorianCalendar) value;
value=g.get(GregorianCalendar.HOUR);
}
}
但是细胞没有显示任何东西,这里怎么了
# 1 楼答案
渲染器的
setValue()
方法实际上不使用它计算的值执行任何操作。我想你想要这个:# 2 楼答案
在我看来,你这样做真的很尴尬
但是我想让他们展示不同的东西。我将更改模型,使其对于这两个不同的单元格具有两个不同的变量,或者至少从
getColumnValue
返回不同的值# 3 楼答案
我已经给出了如何使用自定义渲染器渲染日期对象的工作代码,那么为什么在日期工作正常的情况下使用日历会有很多问题呢
日历具有返回日历日期的方法。因此,这是另一个渲染器的额外代码行。也就是说,您可以从日历中获取日期,然后可以按照其他代码的工作方式对其进行格式化