• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rendering dates from long values in JTable

 
Data Virtue
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to render or display dates from the long values stored in my table model. What is the best way to do this. I've found other info on this but it assumed you had Date objects in the tablemodel.

Sean
 
Data Virtue
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used this code to set my renderer.

return new DefaultTableModel(data,headers){
public Class getColumnClass(int column) { //this code makes sure the proper renderer is used
return DV.idObject(this.getValueAt(0,column));
}
};

In my idObject method I returned Date.class when I ran into a Long column.

Next question, What is the best way to change the date format of the default renderer?

Sean
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Data Virtue", you have previously been warned on multiple occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional, and this is your final warning. Adjust your display name to comply with the required standards prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Failure to comply will result in the removal of your account.

bear
JavaRanch Sheriff
[ November 10, 2008: Message edited by: Bear Bibeault ]
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Data Virtue:
Next question, What is the best way to change the date format of the default renderer?

Use a custom date format.
 
J. Noah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could set the TableCellRenderer for that column to be a custom renderer in which you'd need to override:

public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column){
DateFormat df = new SimpleDateFormat("yyyyMMdd");
this.setText(df.parse(new Date((Long)value)));
return this;
}
[ November 15, 2008: Message edited by: J. Noah ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic