This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Sorting problem of Date column in Jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Sorting problem of Date column in Jtable " Watch "Sorting problem of Date column in Jtable " New topic
Author

Sorting problem of Date column in Jtable

Helen Dong
Greenhorn

Joined: Jan 15, 2013
Posts: 2
Hello, I have an issue with sorting Date column in Jtable which is sort by String type.
How can I achive sorting Date column by click column header?
Please help!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
searching first will generally produce solutions

http://www.coderanch.com/t/563518/GUI/java/Trouble-JTable-sorting-dates
Helen Dong
Greenhorn

Joined: Jan 15, 2013
Posts: 2
Thanks Michael.

Firsr I use getColumnClass to convert String type to Date type, and Date shows as Mon dd, yyyy.
Then use DefaultTableCellRenderer to format to MM/dd/yyyy.

But CellRenderer will skip the column that has change class type.

Do you know how to solve this issue?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Hi Helen,
Welcome to the Ranch!

Are you having the data as a String in your model? In the post link that was provided, you can see that it is recommended to hold the values as 'Date' objects in the model. When that is done, rendering is automatically done by displaying the date in the short format. If you still need your own renderer, you can do that with table.setDefaultRenderer which takes a 'class' type as the first argument.
You can checkout this tutorial page to know more about this.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
 
jQuery in Action, 2nd edition
 
subject: Sorting problem of Date column in Jtable
 
Similar Threads
Jtable sorting; problem with getValueAt
JTable
JTable manipulations
Sorting the JTable
Trouble with JTable sorting dates.