• 0

Java's JTree (TreePath)


Question

Well, this is screwed up! Or at least it seems so.

Now here's an example:

   DefaultMutableTreeNode rNode = new DefaultMutableTreeNode("/");
    DefaultMutableTreeNode cNode = null;
    DefaultTreeModel tModel = new DefaultTreeModel(rNode);

    DefaultMutableTreeNode rNode2 = new DefaultMutableTreeNode("/");
    DefaultMutableTreeNode cNode2 = null;
    DefaultTreeModel tModel2 = new DefaultTreeModel(rNode2);

    JTree jt = new JTree(tModel);
    JTree jt2 = new JTree(tModel2);
   
    cNode = new DefaultMutableTreeNode("demo");
    cNode2 = new DefaultMutableTreeNode("demo");
   
    tModel.insertNodeInto(cNode, rNode, cNode.getChildCount());
    tModel2.insertNodeInto(cNode2, rNode2, cNode2.getChildCount());
   
    jt.expandRow(0);
    jt2.expandRow(0);
   
    TreePath tp = jt.getPathForRow(jt.getRowCount() - 1);

    jt2.setSelectionPath(tp);

    jScrollPaneDir.setViewportView(jt2);

...

Now, as you can see I got 2 exactly the same trees! The little test here shows that even if they have the same structure, I cant get the path from tree #1 and use that on tree #2!!! (??) I was wondering WHY? and how this can be easly solved! The reason I wanna be able to do this is because three #1 is a more complex tree while the other (tree #2) is supposed to be used to be able to select some particual node in that complex #1 tree. I had in mind to then build a tempurary tree to get the TreePath out, since apparently:

jt.setSelectionPath(tp.pathByAddingChild(node);

aint working either!! Which should be damn much nicer (and faster)!

(but please feel free to make this version work! I could have missed something with some object)

Say I got this in tree#1:

/

| -- demo

| | -- file.zip

| | -- file2.zip

| | -- dir2

| -- {dir3}

where {dir3} indicates that dir3 is now selected. Now, if I wanted to add some node to dir2 there is problem. I couldnt really find anything usefull to help me out with that! I NEED the TreePath to that node (dir2). There really just are:

TreePath pathTree = jt.getSelectionPath();

But that assumes you have selected dir2! Annoying. So, I thought (which is WAY better than make java select it by.. say the setSelectionRow(int row); that I could create the path with the "demo" + adding the "dir2" node. Thus do the: jt.setSelectionPath(tp.pathByAddingChild(new DefaultMutableTreeNode("dir2");

But ofcource that isnt working.

Then I thought, I could recreate a new temptree with just the nodes needed. /demo/dir2 in this case and get the path from that (since I here can use the jttemp.getRowCount(); and select the last one (since its only "one" tree, where I wanna select the last child so to speak))

So, to do: TreePath tptemp = jt.getPathForRow(jt.getRowCount());

And voil?! Well, so I thought. Didnt work to use that tptemp for tree #1!

Got damn its frustrating!

Anyone here got time to help me out? Hope this all made sence.. Basicly, what it comes down to. How do I get a TreePath to /demo/dir2 !! that works so I can use that to insert nodes into that if the node itself isnt even selected!? I really wanna avoid using getSelectionPath()!!

Thanks,

Ace

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.