Sorting a JTable with a delegating TableModel

Discussion in 'Java' started by Brian J. Sayatovic, Jul 10, 2003.

  1. I've found nearly every reference to sorting tablesin Java to be one
    of two things: (1) using the delegating model presentign in the Swing
    Tutorial or (2) sorting the actual data in the actual model.

    Because a model is a model of data, it is not unexpected that one
    would use their own custom model that more accurately represents their
    data. This is the approach I'm taking. Thus, my model represents a
    List of objects. Each row represents an object,and each c olumn in
    the row represents the obejct's value for a certain property (i.e.
    name, date, color, etc.).

    When the system needs to delete one of those objects, I delete it from
    the list in my model and fire a table model change event indiciating a
    deleted row.

    However, I think I'm encountering a problem when I wrap my model in a
    SortableTableModel. The problem is that the deleted row could be
    mapped through the sorted table model wrapper. Thus, if I delete
    object 2 (located in row 2 in the real data model), the event fired
    will be that row 2 was deleted. However, from the point of view of
    the JTable, it may have been row 1 that was deleted (assuming teh
    sortable table model had sorted such that row 2 was in the first row).

    I don't believe Sun's example handles this. Perhaps they expect all
    changes to the data model to happen through the model's
    setValueAt(y,x) method. But in my case, the objects in the model
    could be edited in another dialog, and when OK is pressed, I want the
    changes reflected in the JTable representing the list of all objects.
    So, the changes would not go through the sortable table model at all,
    but rather, through the acutal underlying data model.

    I'm guessing the solution is to intercept the table model change
    events with a listener designed to re-map the row indices in the event
    (which may have to be more than 1 event once mapped if the re-sorted
    indices are not contiguous). But I'm concerned that I may be going
    down the wrong path if Su themselves didn't forsee this for their
    example.

    Thoughts?

    Regards,
    Brian.
    Brian J. Sayatovic, Jul 10, 2003
    #1
    1. Advertising

  2. Brian J. Sayatovic

    Dave Glasser Guest

    (Brian J. Sayatovic) wrote on 10 Jul 2003 08:09:21 -0700
    in comp.lang.java.gui:

    >I've found nearly every reference to sorting tablesin Java to be one
    >of two things: (1) using the delegating model presentign in the Swing
    >Tutorial or (2) sorting the actual data in the actual model.
    >
    >Because a model is a model of data, it is not unexpected that one
    >would use their own custom model that more accurately represents their
    >data. This is the approach I'm taking. Thus, my model represents a
    >List of objects. Each row represents an object,and each c olumn in
    >the row represents the obejct's value for a certain property (i.e.
    >name, date, color, etc.).
    >
    >When the system needs to delete one of those objects, I delete it from
    >the list in my model and fire a table model change event indiciating a
    >deleted row.
    >
    >However, I think I'm encountering a problem when I wrap my model in a
    >SortableTableModel. The problem is that the deleted row could be
    >mapped through the sorted table model wrapper. Thus, if I delete
    >object 2 (located in row 2 in the real data model), the event fired
    >will be that row 2 was deleted. However, from the point of view of
    >the JTable, it may have been row 1 that was deleted (assuming teh
    >sortable table model had sorted such that row 2 was in the first row).
    >
    >I don't believe Sun's example handles this. Perhaps they expect all
    >changes to the data model to happen through the model's
    >setValueAt(y,x) method. But in my case, the objects in the model
    >could be edited in another dialog, and when OK is pressed, I want the
    >changes reflected in the JTable representing the list of all objects.
    >So, the changes would not go through the sortable table model at all,
    >but rather, through the acutal underlying data model.
    >
    >I'm guessing the solution is to intercept the table model change
    >events with a listener designed to re-map the row indices in the event
    >(which may have to be more than 1 event once mapped if the re-sorted
    >indices are not contiguous). But I'm concerned that I may be going
    >down the wrong path if Su themselves didn't forsee this for their
    >example.


    I think Sun's example was only intended as a quick proof-of-concept.
    Your guess about intercepting the events of the actual table model is
    the right approach, IMO. But in my experience, unless your JTable is
    strictly read-only--no updating, adding or deleting rows--then using a
    wrapper TableModel to handle sorting can lead to more problems than
    it's worth. Is there a compelling reason *not* to just sort the list
    of data objects? If not, you'll probably save yourself a lot of grief
    by doing it that way, even though you'll lose the benefit of a
    "one-size-fits-all" table sorter.



    ----
    Check out QueryForm, a free, open source, Java/Swing-based
    front end for relational databases.

    http://qform.sourceforge.net
    Dave Glasser, Jul 11, 2003
    #2
    1. Advertising

  3. Brian J. Sayatovic

    Kleopatra Guest

    "Brian J. Sayatovic" wrote:
    >
    > I've found nearly every reference to sorting tablesin Java to be one
    > of two things: (1) using the delegating model presentign in the Swing
    > Tutorial or (2) sorting the actual data in the actual model.
    >
    > Because a model is a model of data, it is not unexpected that one
    > would use their own custom model that more accurately represents their
    > data. This is the approach I'm taking. Thus, my model represents a
    > List of objects. Each row represents an object,and each c olumn in
    > the row represents the obejct's value for a certain property (i.e.
    > name, date, color, etc.).
    >
    > When the system needs to delete one of those objects, I delete it from
    > the list in my model and fire a table model change event indiciating a
    > deleted row.
    >
    > However, I think I'm encountering a problem when I wrap my model in a
    > SortableTableModel. The problem is that the deleted row could be
    > mapped through the sorted table model wrapper. Thus, if I delete
    > object 2 (located in row 2 in the real data model), the event fired
    > will be that row 2 was deleted. However, from the point of view of
    > the JTable, it may have been row 1 that was deleted (assuming teh
    > sortable table model had sorted such that row 2 was in the first row).
    >
    > I don't believe Sun's example handles this.


    Agreed - but then: it's only an example (as Dave already mentioned).

    One problem with sun's example (last time I looked) is in the
    TableModelListener section of TableMap: tableChanged simply routes the
    events from the original model. Instead it should convert the received
    event into the coordinates of the filter and then fire the adjusted
    event btw: the filter should _not_ delegate the listeners to the
    original but manage them itself.

    >
    > I'm guessing the solution is to intercept the table model change
    > events with a listener designed to re-map the row indices in the event
    > (which may have to be more than 1 event once mapped if the re-sorted
    > indices are not contiguous). But I'm concerned that I may be going
    > down the wrong path if Su themselves didn't forsee this for their
    > example.


    Exactly (at least that's the path I go :) As for block inserts/deletes
    (in the original) that are non-contiguous after sorting: IMO the only
    way to cope with them is to fire a dataChanged. If you map such a
    blocked event into several rowsDeleted f.i. then a client listening to
    the filter might try to access parts of the model that are no longer
    available in the original.


    Greetings
    Jeanette
    Kleopatra, Jul 15, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ike

    Renderer in TableModel

    Ike, Oct 25, 2003, in forum: Java
    Replies:
    3
    Views:
    664
  2. Peter Grison
    Replies:
    1
    Views:
    5,553
  3. FET
    Replies:
    2
    Views:
    2,629
    Filip Larsen
    Jan 8, 2005
  4. farseer

    TableModel

    farseer, May 22, 2005, in forum: Java
    Replies:
    1
    Views:
    2,900
    Roland
    May 22, 2005
  5. Deniz Dogan

    JTable/TableModel undoing

    Deniz Dogan, Nov 16, 2006, in forum: Java
    Replies:
    8
    Views:
    4,018
    Thomas Weidenfeller
    Nov 20, 2006
Loading...

Share This Page