🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #1138

adding a node to a JTree

#1138
From: 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);
    }

  }
Technical Details
Message-ID: <40564bf4@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!crtntx1-snh1.gtei.net!chcgil2-snh1.gtei.net!news.bbnplanet.com!nycmny1-snf1.gtei.net!news.gtei.net!colt.net!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,a760aeb930adb421,start
X-Google-Attributes: gid111d45,public
X-Google-ArrivalTime: 2004-03-15 16:39:55 PST
From: "Alan Shiers" <ashiers@hfx.eastlink.ca>
Newsgroups: borland.public.jbuilder.java.swing
Subject: adding a node to a JTree
Date: Mon, 15 Mar 2004 20:36:49 -0400
Lines: 65
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: <40564bf4@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1079397364 24.222.101.2 (15 Mar 2004 16:36:04 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!crtntx1-snh1.gtei.net!chcgil2-snh1.gtei.net!news.bbnplanet.com!nycmny1-snf1.gtei.net!news.gtei.net!colt.net!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail