Updating Jtree problem

I have a JTree which has to refresh (show the new node) on click of button when ever i am adding a new node.

I am adding node by the DefaultTreeModel : insertNodeInto() method.I added the TreeModelListener to treeModel and also called the nodeStructureChanged(parent) method...not working ...

I then tried to override the treeNodesInserted() method by invoking tree.scrollPathToVisible(new TreePath(childNode.getPath()));

...did not work...

I went thru the forum and found only similar answers...Can any one please help....In some URL i found that we need to add TreeExpansionListener ..but i am confused about it.

[645 byte] By [Sruthi.ma] at [2007-10-3 8:17:19]
# 1
U may need to reload the modelafter inserting, call model.reload();This will work
GShankara at 2007-7-15 3:22:34 > top of Java-index,Desktop,Core GUI APIs...
# 2

I did that now ..but still its not reloading..I am sending the code snippet below:

public class TreeModel {

DefaultMutableTreeNode childNode;

DefaultTreeModel jtm;

JTree jt;

//another method calls this method

public void UpdateTree(String sitenm) {

childNode = new DefaultMutableTreeNode((Object) sitenm);

DefaultMutableTreeNode parent=null;

try {

parent = new DefaultMutableTreeNode(jt.getSelectionPath().getLastPathComponent());

} catch (Exception e) {

parent = (DefaultMutableTreeNode) jtm.getRoot();

}

jtm.insertNodeInto(childNode, parent, parent.getChildCount());

jtm.nodeStructureChanged(parent);

//jt.scrollPathToVisible(new TreePath(childNode.getPath()));

//jt.updateUI();

jtm.reload();

}

public class TreeListener implements TreeModelListener {

public void treeNodesChanged(TreeModelEvent tme) {

}

public void treeNodesInserted(TreeModelEvent tme) {

jtm.reload();

jt.scrollPathToVisible(new TreePath(childNode.getPath()));

}

public void treeNodesRemoved(TreeModelEvent tme) {

}

public void treeStructureChanged(TreeModelEvent tme) {

}

}

I tried using reload method in both methods and also immediately after the insert action.Its working partially ..like when no node is selected it gets added to root node of tree..but when i select a node in tree and then try adding to it,Its not refreshing.Is there anything else that i will have to do.

Sruthi.ma at 2007-7-15 3:22:34 > top of Java-index,Desktop,Core GUI APIs...
# 3

In your code snippet, the DefaultTreeModel and JTree are no where related

Modify the first three lines of code as follows

DefaultMutableTreeNode childNode = new

DefaultMutableTreeNode("Root");

DefaultTreeModel jtm = new DefaultTreeModel(childNode);

JTree jt = new JTree(jtm);

Now it will work

GShankara at 2007-7-15 3:22:34 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thank you very much for the suggestion..but i guess my tree will not be created without those three lines.I wrote it in program, but i didnot to write it here...Its like this(in my program):

DefaultMutableTreeNode node=new DefaultMutableTreeNode("Site");

//add all the child nodes to above node.and then

DefaultTreeModel dtm=new DefaultTreeModel(node);

JTree tree= new JTree(dtm);

after this i am trying to add new nodes and that is not getting updated in the tree....Please check and let me know if there is any mistake....

Sruthi.ma at 2007-7-15 3:22:34 > top of Java-index,Desktop,Core GUI APIs...
# 5
Yes you are correctYoui cant create new instance of DefaultMutableTreeNode before insertingTry replacing the line inside try block with the followingparent = (DefaultMutableTreeNode)jt.getSelectionPath().getLastPathComponent();
GShankara at 2007-7-15 3:22:34 > top of Java-index,Desktop,Core GUI APIs...
# 6

It worked ...Thank you very very much......

I tried to check what my mistake is :

Actually i am getting the required node with "jt.getSelectionPath().getLastPathComponent()" . But ,by writing "new" keyword i am creating a new node and so this new node is not located in tree....Corrrect or anything else?

Sruthi.ma at 2007-7-15 3:22:34 > top of Java-index,Desktop,Core GUI APIs...
# 7
s that correct
GShankara at 2007-7-15 3:22:34 > top of Java-index,Desktop,Core GUI APIs...