🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
7 messages
7 total messages Started by Alan Shiers Tue, 16 Mar 2004 00:36
adding a node to a JTree
#1138
Author: Alan Shiers
Date: Tue, 16 Mar 2004 00:36
63 lines
3030 bytes
Hi there,

I'm working with a project that involves a JTree.  Once I have populated the
tree I have a method called deleteLeafNode() which works just fine. When I
right-click on the node in question, bringing up a popup menu to delete the
node, deleteLeafNode() proceeds to remove the node from that branch of the
tree when I call model.nodeStructureChanged(parent). As expected, the node
disappears from the tree.

I have another method called createLeafNode() which uses the same steps as
deleteLeafNode() and makes a call to model.nodeStructureChanged(parent),
however, in this case a new node doesn't appear in the JTree.

Any ideas as to how to make the node appear where it should?

Please advise,

Alan

The two methods follow:

public void createLeafNode()
  {
     jTree1.repaint();
     TreePath path = ref.FileSaveAsTreePath;
     if(path == null)
        return;
     //System.out.println("path is: " + path.toString());
     DefaultTreeNode parent = getTreeNode(path);
     System.out.println("parent is " + parent.toString());
     //Create the child node
     IconData idata = new IconData(ICON_LEAF, new FileNode(new
SpecialFile(ref.FileOpen)));
     DefaultTreeNode node = new DefaultTreeNode(idata);
     parent.add(node);
     //Tell the model that the tree structure has changed
     model.nodeStructureChanged(parent);
     path = path.pathByAddingChild(node);
     jTree1.scrollPathToVisible(path);
     //jTree1.repaint();


  }

  public void deleteLeafNode()
  {
    //jTree1.repaint();
    TreePath path = jTree1.getSelectionPath();
    if(path == null)
       return;
    DefaultTreeNode treeNode = getTreeNode(path);
    FileNode node = getFileNode(treeNode);
    if(node == null)
        return;
    SpecialFile file = node.getFile();
    if(file != null && !file.isDirectory())
    {
      DefaultTreeNode parent = (DefaultTreeNode)treeNode.getParent();
      treeNode.removeFromParent();
      model.nodeStructureChanged(parent);
    }

  }
Re: adding a node to a JTree
#1140
Author: Alan Shiers
Date: Tue, 16 Mar 2004 19:17
80 lines
3728 bytes
Hi,

I neglected to mention that I'm using a SaveAs dialog box that contains a
duplicate copy of the JTree (directories only), for the user to select where
they wish to save the open file.  Obviously, they should be able to save the
file in any directory on the tree.  The SaveAs dialog box sets the variable
FileSaveAsTreePath of type TreePath.

My main problem, I am discovering, is that the main JTree on the main
interface doesn't receive an actual click of the node from the mouse,
thereby not triggering a TreeSelectionEvent as apposed to when I try to
delete a node.  So, I need some way to programmatically tell the JTree to
select the node that corresponds with the one selected in the SaveAs dialog
box.  I am able to pass a TreePath object to FileSaveAsTreePath variable and
even use this to call jTree1.setSelectionPath(...) but that didn't help.

I even went as far as creating a TreeSelectionEvent and calling the
valueChanged() method of the TreeSelectionListener like so:

SelectionListener.valueChanged(new TreeSelectionEvent(jTree1, path, true,
null, null));

Again, that didn't do anything to help.  I'm running out of ideas guys.

Please help,

Alan


> The two methods follow:
>
> public void createLeafNode()
>   {
>      jTree1.repaint();
>      TreePath path = ref.FileSaveAsTreePath;
>      if(path == null)
>         return;
         //jTree1.setSelectionPath(path);

        //SelectionListener.valueChanged(new TreeSelectionEvent(jTree1,
path, true, null, null));

>      //System.out.println("path is: " + path.toString());
>      DefaultTreeNode parent = getTreeNode(path);
>      System.out.println("parent is " + parent.toString());
>      //Create the child node
>      IconData idata = new IconData(ICON_LEAF, new FileNode(new
> SpecialFile(ref.FileOpen)));
>      DefaultTreeNode node = new DefaultTreeNode(idata);
>      parent.add(node);
>      //Tell the model that the tree structure has changed
>      model.nodeStructureChanged(parent);
>      path = path.pathByAddingChild(node);
>      jTree1.scrollPathToVisible(path);
>      //jTree1.repaint();
>
>
>   }
>
>   public void deleteLeafNode()
>   {
>     //jTree1.repaint();
>     TreePath path = jTree1.getSelectionPath();
>     if(path == null)
>        return;
>     DefaultTreeNode treeNode = getTreeNode(path);
>     FileNode node = getFileNode(treeNode);
>     if(node == null)
>         return;
>     SpecialFile file = node.getFile();
>     if(file != null && !file.isDirectory())
>     {
>       DefaultTreeNode parent = (DefaultTreeNode)treeNode.getParent();
>       treeNode.removeFromParent();
>       model.nodeStructureChanged(parent);
>     }
>
>   }
>
>
Re: adding a node to a JTree
#1142
Author: Paul Furbacher [TeamB]
Date: Tue, 16 Mar 2004 22:26
40 lines
2070 bytes
Alan Shiers wrote:

> [...].
> 
> My main problem, I am discovering, is that the main JTree on the main
> interface doesn't receive an actual click of the node from the mouse,
> thereby not triggering a TreeSelectionEvent as apposed to when I try to
> delete a node.  So, [...]

JTree has methods for translating x,y into rows, path, etc.
The mouse event which pops up your menu gives you x and y.
Pass those values into your method.  I think that should
solve your problem.

>>public void createLeafNode(int x, int y)
>>  {
>>     //jTree1.repaint();

   TreePath path = jTree1.getPathForLocation(x, y);

>>     if(path == null)
>>        return;
>>[...]
>>  }


-- 


Paul Furbacher (TeamB)

   Save time, search the archives:
        http://www.borland.com/newsgroups/ngsearch.html

   Is it in Joi Ellis's Faq-O-Matic?
        http://www.visi.com/~gyles19/fom-serve/cache/1.html

   Finally, please send responses to the newsgroup only.
   That means, do not send email directly to me.
   Thank you.
Re: adding a node to a JTree
#1141
Author: Paul Furbacher [TeamB]
Date: Tue, 16 Mar 2004 22:30
26 lines
1621 bytes
Paul Furbacher [TeamB] wrote:

> [...]

In addition, consider Googling

   jtree getpathforlocation node select popup

See the jGuru hit (should be the first one).
Others may also be relevant.


-- 


Paul Furbacher (TeamB)

   Save time, search the archives:
        http://www.borland.com/newsgroups/ngsearch.html

   Is it in Joi Ellis's Faq-O-Matic?
        http://www.visi.com/~gyles19/fom-serve/cache/1.html

   Finally, please send responses to the newsgroup only.
   That means, do not send email directly to me.
   Thank you.
Re: adding a node to a JTree
#1143
Author: Alan Shiers
Date: Tue, 16 Mar 2004 23:56
60 lines
3001 bytes
Hi Paul,

I think you misunderstand my problem.  I have no problem creating a TreePath
object as the SaveAs dialog box, which has a duplicatate JTree structure
(directories only), produces one for me upon selection of a node in the
JTree of the SaveAs dialog box.  I then pass this TreePath object to an
instance variable into another class to hold.  At no time have I actually
clicked on a node of the JTree on the main interface.  I have only the
TreePath object and need to use that to signal to JTree on main interface to
select the node specified by the TreePath object.

Now, do you see?

How do I select a node programmatically using a TreePath object?

Alan


"Paul Furbacher [TeamB]" <pfurbacher@mac.com> wrote in message
news:40577f00@newsgroups.borland.com...
> Alan Shiers wrote:
>
> > [...].
> >
> > My main problem, I am discovering, is that the main JTree on the main
> > interface doesn't receive an actual click of the node from the mouse,
> > thereby not triggering a TreeSelectionEvent as apposed to when I try to
> > delete a node.  So, [...]
>
> JTree has methods for translating x,y into rows, path, etc.
> The mouse event which pops up your menu gives you x and y.
> Pass those values into your method.  I think that should
> solve your problem.
>
> >>public void createLeafNode(int x, int y)
> >>  {
> >>     //jTree1.repaint();
>
>    TreePath path = jTree1.getPathForLocation(x, y);
>
> >>     if(path == null)
> >>        return;
> >>[...]
> >>  }
>
>
> -- 
>
>
> Paul Furbacher (TeamB)
>
>    Save time, search the archives:
>         http://www.borland.com/newsgroups/ngsearch.html
>
>    Is it in Joi Ellis's Faq-O-Matic?
>         http://www.visi.com/~gyles19/fom-serve/cache/1.html
>
>    Finally, please send responses to the newsgroup only.
>    That means, do not send email directly to me.
>    Thank you.
Re: adding a node to a JTree
#1146
Author: Paul Furbacher [TeamB]
Date: Wed, 17 Mar 2004 13:34
64 lines
3270 bytes
Alan Shiers wrote:

> I think you misunderstand my problem.  I have no problem creating a TreePath
> object as the SaveAs dialog box, which has a duplicatate JTree structure
> (directories only), produces one for me upon selection of a node in the
> JTree of the SaveAs dialog box.  I then pass this TreePath object to an
> instance variable into another class to hold.  At no time have I actually
> clicked on a node of the JTree on the main interface.  I have only the
> TreePath object and need to use that to signal to JTree on main interface to
> select the node specified by the TreePath object.
> 
> Now, do you see?

Yes, I see that I misread it.  I was incorrectly
thinking that there was a mouse event related
to some popup which your methods weren't receiving.
Got it now.

> How do I select a node programmatically using a TreePath object?

There are some unknowns here, so I'm going
to do some guessing.

If I browse JFileChooser, I don't find
any reference to "treepath".  Are we dealing
with a Save As dialog of your own crafting?

Also, I notice that you commented out

   jTree1.setSelectionPath(path);

Is that because that didn't select anything,
contrary to what you expected?  If so, that
leads me to think that the tree path which
you get back from the dialog is not filled
with the same node objects as the ones in
the tree you are trying to maintain.  And if
that is the case, my guess is that you
will have to walk the tree, starting at the
root, asking for a child iterator/enumerator
for the node, and asking each node what it's
user object is, and then comparing the value
of importance for a match.

   treepath.getPath()

gives you an array.  You can use that to compare
to each node as you iterate through each level.


-- 


Paul Furbacher (TeamB)

   Save time, search the archives:
        http://www.borland.com/newsgroups/ngsearch.html

   Is it in Joi Ellis's Faq-O-Matic?
        http://www.visi.com/~gyles19/fom-serve/cache/1.html

   Finally, please send responses to the newsgroup only.
   That means, do not send email directly to me.
   Thank you.
Re: adding a node to a JTree
#1147
Author: Alan Shiers
Date: Wed, 17 Mar 2004 17:23
40 lines
2543 bytes
> There are some unknowns here, so I'm going
> to do some guessing.
>
> If I browse JFileChooser, I don't find
> any reference to "treepath".  Are we dealing
> with a Save As dialog of your own crafting?

Yes, that is the case.

>
> Also, I notice that you commented out
>
>    jTree1.setSelectionPath(path);

I only commented it out because it didn't do what I had expected. I may
uncomment it if it helps to achieve the end result.  I was simply hoping
there was a method I could call that, given the TreePath object, would
simply automatically select the desired node.  But that wasn't the case with
setSelectionPath(path).

>
> Is that because that didn't select anything,
> contrary to what you expected?  If so, that
> leads me to think that the tree path which
> you get back from the dialog is not filled
> with the same node objects as the ones in
> the tree you are trying to maintain.

The JTree in the SaveAs dialog box is identicle to the one on the main
interface, except that it shows only the directories, no leaf nodes.  So I
know that whatever node is chosen in the SaveAs dialog box, the same node
exists in the JTree on the main interface.  Once a node is chosen in the
SaveAs dialog box I capture the TreePath object and send it to an instance
variable in another class for retrieval by the method I was showing you
earlier.

Hope this helps clarify things,

Alan
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads