Search code examples
javaswingjtableswingxjxtreetable

How to make a JXTreeTable sort its top elements


I'm aware (I've looked at the sources ;) ) that sorting on JXTreeTable has been disabled.

However, I would like to allow sorting on all columns based only on the values of the direct children of the root node.

Say I have a structure like this:

Name       / Date       / File UID
(Root)
|- Mr. X   / 1996/10/22 / AE123F6D
|--- File1 / 2012/01/10 / DFC2345Q
|--- File2 / 2012/01/11 / D321FEC6
|- Mrs. Y  / 1975/03/03 / G2GF35EZ
|--- File3 / 2012/02/29 / 35GERR32
|--- File4 / 2012/01/22 / LKJY2342
.
.
.

What I want to achieve is sorting on the 3 columns only on first level nodes. Say I want to sort it by ascending date, it would end up like that:

Name       / Date       / File UID
(Root)
|- Mrs. Y  / 1975/03/03 / G2GF35EZ
|--- File3 / 2012/02/29 / 35GERR32
|--- File4 / 2012/01/22 / LKJY2342
|- Mr. X   / 1996/10/22 / AE123F6D
|--- File1 / 2012/01/10 / DFC2345Q
|--- File2 / 2012/01/11 / D321FEC6
.
.
.

As I see it, it ressembles to simple table sorting (so the hierarchical limitation invoked for disabling sorting is lifted).

I see 2 possibilities to do that:

  1. Re-enable the sorting mechanism available in JXTable to benefit from everything that is already implemented (sorter, sorting by clicking on the headers,...) and only sort nodes of the first level (as their children would still have the same parent).
  2. Implement the basic stuff I need (sorting by clicking on the headers mostly), have a list of model-row-ids in JXSortableTreeModel that I sort and override convertRowIndexToModel which would allow (I think) to display my item sorted as I want.

My favourite is of course the first one but I don't know how to achieve it. The first limitation is that I don't know how to re-enable sorting as JXTreeTable overrides setSortable() (and all related methods) and I don't know how to directly access the implementation of the method in JXTable (that's a Java problem). I could simply copy the code of JXTreeTable and remove the overrides but that's not an option in my opinion.

Say I solve this, how would I limit the sorting to the first-level nodes? Even after looking at the source, I'm rather clueless about how to do that.

With the second one, I loose all the benefits of existing code but I see a practical way to achieve what I want (for now, at least. Who knows if won't want to filter the data on all rows?).

Is there another way to go? If not, which one of my solutions should I choose? Any insightful comments?

Many thanks in advance!


Solution

  • maybe sorting TreeTable following way(s), by aephyr, btw this Swing Guru played with Nimbus Look and Feel too, code for funny Nimbus included

    enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here