Article View: borland.public.jbuilder.java.swing
Article #9386Re: Ordering of Nodes in JTree
From: John P. McGrath
Date: Mon, 03 Apr 2000 00:00
Date: Mon, 03 Apr 2000 00:00
49 lines
2476 bytes
2476 bytes
On Mon, 3 Apr 2000 13:39:27 -0400, Tamy Staub wrote:
> I am working on an App that uses a JTree, with a DefaultTreeModel,
> with customized nodes derived from DefaultMutableTreeNode. As nodes
> are added they are displayed in the JTree in alphabetical ordering
> by node labels under their parent nodes.
:
> Is there any simple way to control the order in which the nodes are
> displayed in the tree?
I do not think there is any logic in JTree or related classes that sorts
the nodes or insert a node according to some ordering. The normal method
used to insert a node into a DefaultTreeModel is:
insertNodeInto( MutableTreeNode newChild, MutableTreeNode parent,
int index);
I would assume that somewhere in your program there is logic that inserts
a node into another node based on the String associated with the node.
You will have to find this code, and change it behavior.
Whenever you have a container that manages the ordering of its children,
it is usually a good idea to parameterize the comparison logic. There
are three common ways to deal with this:
1) Require that the objects added to the container implement the
Comparable interface, and then use the compareTo() method to determine
the ordering of the children. This is usually referred to as the
"natural ordering".
2) Add a Comparator attribute to the container, and use its compare()
method to compare the objects.
3) Combine #1 and #2: If a Comparator is specified then use it.
Otherwise use the Comparable interface of the objects.
> Can 2 different criteria for ordering be used in the same tree?
> Any suggestions on how to accomplish this? Thanks! T.
Yes, but not at the same time. :o)
If you have parameterized the comparison as I described above, then you
need only change the Comparator for the JTree. Of course, your JTree
should re-sort itself whenever the Comparator is changed.
--
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID:
<MPG.13532e78f5b1caa1989855@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<8cakem$aeb12@bornews.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,2e30d3022aeaffe X-Google-Attributes: gid111d45,public From: John P. McGrath <JPMcGrath@fast.net> Subject: Re: Ordering of Nodes in JTree Date: 2000/04/03 Message-ID: <MPG.13532e78f5b1caa1989855@forums.borland.com> X-Deja-AN: 606392420 References: <8cakem$aeb12@bornews.borland.com> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing