I have this problem with my JTree. I have implemented a JTree, with a root
node, where I add all my nodes on the first level. Since I have a bunch of
trees to show, I need to change my JTree, according to element that has
been selected in a JList. Now the problem is, that if I remove all elements
from the root node (using removeAllChildren() ), and add the new nodes to
the root node, according to the newly selected element, that the first
build up tree keeps showing with the old nodes instead of the newly added.
So how can I completely empty my JTree and build up a new one?
thanx in advance,
Tim
don't work with tree nodes, but do it with TreeModel.
--
Andrei Kouznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
> don't work with tree nodes, but do it with TreeModel.
I have found some information here:
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#sel
ect
But there seems to be a problem when adding the nodes. You need to know
the TreePath of a node if you want to add a node to that node. If your
node where you want to add a new node to, is selected, you can get this
easily done, but if that node is not selected, how can you derife the
TreePath to it? I have tried the method getPath(), but that one returns
a node, not a TreePath?
thanx
Tim
> "ak" <sp...@imagero.com> schreef op wo, 04 aug 2004 22:43:24 GMT in
> news:ceroq1$hce$1...@online.de:
>
>> don't work with tree nodes, but do it with TreeModel.
>
> I have found some information here:
> http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#sel
> ect
Ok, nevermind that last one, you don't need the path, and I found a way to
get it anyways. But, I still wonder, how can I instantly remove all nodes
from the tree? Because, when I "refill" the tree, the old elements are
still showing. What's even more: if I close my program and restart it, all
nodes are showing from last time, plus the new ones?
Invoking removeAllChildren() on the root node still doesn't seem to do the
trick. It can't be possible that I have to remove all nodes one by one,
using removeNodeFromParent from the DefaultTreeModel, can it?
Furthermore, if I add a new node to the tree, it doesn't show instantly. I
have to select another item in my list, and then again the first item so
the tree is rebuilded, and then it shows up. Nevertheless I use the
insertNodeInto(childNode, parent, parent.getChildCount()); method, like in
the example. Any idea why it doesn't show up, when I do this?
thanx
Some as before: tae the model...code:
DefaultTreeModel model = (DefaultTreeModel) myTree.getModel();
model.insertNodeInto(newChildNode,parentNode,position); //<-- add to end:
position = parentNode.getChildCount() ) ;
Andreas
If you mean the getpath() method of DefaultMutableTreeNode, it returns an array
of TreeNodes not a TreeNode. You can construct a new TreePath from it, using
the constructor of Treepath that takes an Object[] as its argument.
BK
> Some as before: tae the model...code:
>
> DefaultTreeModel model = (DefaultTreeModel) myTree.getModel
();
>
> model.insertNodeInto(newChildNode,parentNode,position); //
<-- add to
> end: position = parentNode.getChildCount() ) ;
please see my other message (reply on the previous one), thanx
> If you mean the getpath() method of DefaultMutableTreeNode, it returns an
array
> of TreeNodes not a TreeNode. You can construct a new TreePath from it,
using
> the constructor of Treepath that takes an Object[] as its argument.
>
public TreePath getPathForNode(TreeNode node) {
ArrayList list = new ArrayList();
// Add all nodes to list
while (node != null) {
list.add(node);
node = node.getParent();
}
Collections.reverse(list);
// Convert array of nodes to TreePath
return new TreePath(list.toArray());
}
> public TreePath getPathForNode(TreeNode node) {
> ArrayList list = new ArrayList();
> // Add all nodes to list
> while (node != null) {
> list.add(node);
> node = node.getParent();
> }
> Collections.reverse(list);
>
> // Convert array of nodes to TreePath
> return new TreePath(list.toArray());
> }
thanx for the information, the adding works fine now. The only problem I
still have, is that I can't seem to empty my root node (or
DefaultTreeModel) easily so I can build up a new one that has absolutely
nothing to see with the previous one. It can't be possible that the only
way to empty a whole tree is to delete all the nodes, one by one, with
removeNodeFromParent from DefaultTreeModel?
> http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#sel
> ect
Note that you can preserve long URL's
for newsreaders using '<' '>', vis.
<http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#select>
--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
> thanx for the information, the adding works fine now. The only problem
> I still have, is that I can't seem to empty my root node (or
> DefaultTreeModel) easily so I can build up a new one that has
> absolutely nothing to see with the previous one. It can't be possible
> that the only way to empty a whole tree is to delete all the nodes,
> one by one, with removeNodeFromParent from DefaultTreeModel?
ok, after googling for quite a time now, I've found this: If you delete
the children from the root node, they are actually all deleted, but you
have to make it clear to the DefaultModelTree, by using: .reload(); If
you don't know that, you can search a long time before you find out why
the tree is not updating properly.
thanx for all of your reply's.
Tim