So I have been trying to use the example from Tutorial and change it so it fits my program. The getColumnValue method returns the object that holds the information that is supposed to be displayed. Is this the way to go or should it rather return the actual String to be displayed. I guess not because that way I would mix the presentation with the data, which I was trying to avoid.
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();
开发者_C百科 }
}
The column 0 and 1 contain a GregorianCalendar object, but I want column 0 to show the date and 1 to show the time.
So I know using cellRenderers can help here.
This is what I tried.
public class DateRenderer extends DefaultTableCellRenderer {
public DateRenderer() { super(); }
public void setValue(Object value) {
GregorianCalendar g =(GregorianCalendar) value;
value=g.get(GregorianCalendar.HOUR);
}
}
But the cell doesnt show anything, what is wrong here?
To me it looks really awkward that you have
if(column == 0) return issue.getDate();
else if(column == 1) return issue.getDate();
but want them to display different things. I would change the model so that either it had two different variables for these two different cells, or that it least return different values from the getColumnValue
.
Your renderer's setValue()
method doesn't actually do anything with the value it computes. I think you want this:
public void setValue(Object value) {
GregorianCalendar g =(GregorianCalendar) value;
super.setValue(g.get(GregorianCalendar.HOUR));
}
I already gave you working code on how to render a Date object using a custom renderer, so why do you have multiples questions on using a Calendar when a Date works just fine?
A Calendar has a method to return the Date of the Calendar. So its one extra line of code to your other renderer. That is you get the Date from the Calendar and then you can format it the same way the other code works.
精彩评论