Results 1 to 2 of 2
  1. #1
    mattj is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default sorting Dates with TableSorter

    I have made a sorter and a table using

    Java Code:
    model =new DefaultTableModel();
    sorter = new TableSorter(model);
    table = new JTable(sorter);
    And i have 6 columns with the following types

    Integer, String, String, String (representing a Date), String (representing a Date), double

    And the problem is, since i used SimpleDateFormat to format my dates into dd/MM/yyyy they are of type String in the table. I want to know how i can write a comparator that sorts the dates correctly, and also how to implement it with TableSorter.java available from java.sun.com/docs/books/tutorial/uiswing/examples/components/TableSorterDemoProject/src/components/TableSorter.java

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Writing a comparator shouldn't be too dificult; you have to parse the string and do some Calendar/Date math.
    I would leave the Date types in the TableModel (and let java do the math), set a custom renderer on the two Date columns and use your SimpleDateFormat to format the Date values in the renderer.

Similar Threads

  1. TableSorter
    By mattj in forum New To Java
    Replies: 3
    Last Post: 09-16-2008, 11:48 PM
  2. How to Compare two Dates
    By Java Tip in forum java.util
    Replies: 1
    Last Post: 06-24-2008, 08:05 AM
  3. No fo days between two dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 10:06 AM
  4. help with dates and time
    By osval in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 01:41 PM
  5. differences between 2 dates
    By cecily in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •