This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes JTree problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree problem" Watch "JTree problem" New topic

JTree problem

Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
Hello All,

I've got an app where I'm trying to dynamically insert nodes into a JTree
So, I have a button whose event handler looks like this


TreePath parentPath = m_app.jTree1.getSelectionPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode)parentPath.getLastPathComponent();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("foo");
m_app.treeModel.insertNodeInto(child, node, 0);
m_app.jTree1.scrollPathToVisible(new TreePath(child.getPath()));
catch(Exception e)
System.out.println("Error in jButton1ActionPerformed = " + e.toString());

where "m_app.jTree1" is the tree
when I click the button a node get's properly inserted into the selected node as long as that node has no children.
If it has children, nothing happens
no node
no exception
no nothing
I suspect there's something wrong with the way I'm getting the path to the currently selected node.

note: when the app starts, it build the initial tree from info in a database and everything works fine. it's just when I later try to dynamically add more nodes that I have a problem

Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
figured it out
had to do with using the root node in the tree constructor rather than the treemodel

I agree. Here's the link:
subject: JTree problem