Article View: borland.public.jbuilder.java.swing
Article #1140Re: adding a node to a JTree
From: Alan Shiers
Date: Tue, 16 Mar 2004 19:17
Date: Tue, 16 Mar 2004 19:17
80 lines
3728 bytes
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);
> }
>
> }
>
>
Technical Details
Message-ID:
<405752b0@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!zeus.visi.com!news-out.visi.com!petbe.visi.com!news.octanews.net!in.100proofnews.com!in.100proofnews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<40564bf4@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,a760aeb930adb421 X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2004-03-16 11:24:11 PST From: "Alan Shiers" <ashiers@hfx.eastlink.ca> Newsgroups: borland.public.jbuilder.java.swing References: <40564bf4@newsgroups.borland.com> Subject: Re: adding a node to a JTree Date: Tue, 16 Mar 2004 15:17:45 -0400 Lines: 82 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 24.222.101.2 Message-ID: <405752b0@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1079464624 24.222.101.2 (16 Mar 2004 11:17:04 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!zeus.visi.com!news-out.visi.com!petbe.visi.com!news.octanews.net!in.100proofnews.com!in.100proofnews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail