Updating a JTree

Hi, i'm relatively new to java, and i have a question about JTrees. I have created a GUI that uses a JTree with a root and a bunch of leaf nodes. In my program, the user must be able to click a button and add a new leaf node to the JTree. However, I dont know how to update the JTree so that the user can see the new nodes right after he/she clicks the button. Please do not use alot of jargon or condesed code in your replies, or i probably wont understand it. Thanks in advance.

[488 byte] By [Tommasoa] at [2007-10-2 4:03:10]
# 1

To insert a node at the end of the roots children and have the tree update so you are shown it has been added:

DefaultMutableTreeNode root = (DefaultMutableTreeNode)tree.getModel().getRoot();

int childCnt = root.getChildcount();

DefaultMutableTreeNode newChild = new DefaultMutableTreeNode("new last child node");

root.insertNodeInto(newChild,root,childCnt+1);

insertNodeInto automatically calls nodesWereInserted, which then calls fireNodesInserted. These are the methods that update the GUI to reflect your new node in the model.

fossill1a at 2007-7-15 23:25:40 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for the help, but for some reason i could not get the last line to compile. I also looked it up in the DefaultTreeModel class interface and it looked correct.

root.insertNodeInto(newChild,root,childCnt+1);

here is the error my compiler gives me:

cannot resolve symbol - method insertNodeInto(javax.swing.tree.DefaultMutableTreeNode, javax.swing.tree.DefaultMutableTreeNode, int)

Do i need to import a special class maybe?

Tommasoa at 2007-7-15 23:25:40 > top of Java-index,Desktop,Core GUI APIs...
# 3

Well, for sure you'd need to

import javax.swing.*;

import javax.swing.tree.*;

Also check that the version of javac you're using is more recent. I guess you could be using an old enough version that that particular function may not have existed yet?

Oh sorry, I see it. that method is not part of the node but of the model.

((DefaultTreeModel)tree.getModel()).insertNodeInto(newChild,root,childCnt+1);

I'm not sure if the +1 part will cause an ArrayOutOfBoundException. Give it a shot.

fossill1a at 2007-7-15 23:25:40 > top of Java-index,Desktop,Core GUI APIs...
# 4

Ok, so the new code compiled, but the program terminates when i click the button to add a new node.

This is the error it Gives Me:

java.lang.ArrayIndexOutOfBoundsException: 4 > 3

at java.util.Vector.insertElementAt(Vector.java:557)

at javax.swing.tree.DefaultMutableTreeNode.insert(DefaultMutableTreeNode.java:177)

at javax.swing.tree.DefaultTreeModel.insertNodeInto(DefaultTreeModel.java:218)

at SuperCrap$clickListener.actionPerformed(SuperCrap.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)

at java.awt.Component.processMouseEvent(Component.java:5100)

at java.awt.Component.processEvent(Component.java:4897)

at java.awt.Container.processEvent(Container.java:1569)

at java.awt.Component.dispatchEventImpl(Component.java:3615)

at java.awt.Container.dispatchEventImpl(Container.java:1627)

at java.awt.Component.dispatchEvent(Component.java:3477)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)

at java.awt.Container.dispatchEventImpl(Container.java:1613)

at java.awt.Window.dispatchEventImpl(Window.java:1606)

at java.awt.Component.dispatchEvent(Component.java:3477)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

Tommasoa at 2007-7-15 23:25:40 > top of Java-index,Desktop,Core GUI APIs...
# 5
Yep, I suspected that. I guess when you add to the end of a node, you'll just want to call add on the node itself, or whatever the method is called instead of inserting.
fossill1a at 2007-7-15 23:25:40 > top of Java-index,Desktop,Core GUI APIs...
# 6
I'm sorry, but I dont really understand what you are saying.
Tommasoa at 2007-7-15 23:25:40 > top of Java-index,Desktop,Core GUI APIs...
# 7
Then I guess you'll just have to RTFM. Or wait for the next sucker to give you what you want.
fossill1a at 2007-7-15 23:25:40 > top of Java-index,Desktop,Core GUI APIs...