> Ive been wrestling for nearly a day with what ought to be a simple
> algorithm. I'm trying to find leafs on a JTree where the data emanates from
[quoted text clipped - 4 lines]
> specified below. Ive enclosed my code (the current state of it anyways!) --
> if anyone has any insight on this, please share it with me. THank you -Ike
First a few general observations:
- You code is as messy as your problem description. Actually, just
dumping this mess onto us, without even cleaning it up and formating it
in a reasonable way is a little bit cheeky. You might want to sit down
for a minute with some pencil and paper and write a simplified
description. I think I know what you want, but I am not 100% sure.
- I don't get the idea of the LeafInfo class. I will ignore it.
- You should make sure that getAllowsChildren() returns correct
information. It should return true for folder-type nodes, and false for
any other data.
> private void killTheKids(MutableTreeNode nodetokill, int fileno){
> // jTree.collapsePath((TreePath)jTree.getModel().getRoot());
> Enumeration e = nodetokill.children();
Enumerating and removing nodes at the same time gets you in trouble. The
enumerator makes no guarantee that it will work correctly if you remove
elements while enumerating.
> while(e.hasMoreElements()) {
> MutableTreeNode child = (MutableTreeNode) e.nextElement();
> if(child.isLeaf()){
Here you look ahead in your recursion for no apparent reason.
> LeafInfo nodeInfo =(LeafInfo)
> (((DefaultMutableTreeNode)child).getUserObject());
[quoted text clipped - 8 lines]
> //while if the the parent of nodetokill is either a
> leaf or has no more children, remove it
This does not make sense. The parent can't be a leaf. A leaf has no
children, if the TreeNode data structure is intact. If you find this in
your tree you have managed to butcher your tree to an extend that the
tree is basically worthless.
> MutableTreeNode m = nodetokill;
nodetokill is already a MutableTreeNode. Why are you assigning it to
another reference?
> while(m!=null && (m.isLeaf() ||
> m.getChildCount()==0)){
[quoted text clipped - 4 lines]
>
> }
In theory this could be replaced with
m.removeFromParent();
but you are in fact disconnecting your current node from the tree while
still recursing in that branch.
Or in short, something like:
protected boolean canBeRemoved(TreeNode node, ...) {
return node.isLeaf() &&
( node.getAllowsChildren() // empty folder-type node
|| // no folder, check for special condition
|| // whatever other criteria
|| // qualifies for removal
);
}
public boolean removeAll(MutableTreeNode node, ...) {
int n = node.getChildCount();
int i = 0;
while(i < n) {
if(removeAll((MutableTreeNode)node.getChildAt(i), ...))
{
node.remove(i);
n--;
} else {
i++;
}
}
return canBeRemoved(node, ...);
}

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
Ike - 15 Mar 2005 20:01 GMT
Thanks Thomas --you;re right, sorry the code has been "dumped," as is --
I've been too close to it thje last few hours to realise that until you
pointed it out. Thanks for your help. -Ike
> - You code is as messy as your problem description. Actually, just
> dumping this mess onto us, without even cleaning it up and formating it
> in a reasonable way is a little bit cheeky. You might want to sit down
> for a minute with some pencil and paper and write a simplified
> description. I think I know what you want, but I am not 100% sure.