aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Rendering dates from long values in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Rendering dates from long values in JTable" Watch "Rendering dates from long values in JTable" New topic
Author

Rendering dates from long values in JTable

Data Virtue
Greenhorn

Joined: Apr 28, 2007
Posts: 8
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

Joined: Apr 28, 2007
Posts: 8
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

Joined: Jan 10, 2002
Posts: 61437
    
  67

"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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
J. Noah
Greenhorn

Joined: Nov 15, 2008
Posts: 9
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 ]

<a href="http://blog.expensivedna.com" target="_blank" rel="nofollow">http://blog.expensivedna.com</a>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Rendering dates from long values in JTable