Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTree is not updating

843805Sep 13 2005 — edited Sep 14 2005
HI ,
I am getting difficulty in refreshing the mytree .What is wrong I am unable to find out .
.Here the already I have rootNode and it's three subnode subnode1,subnode2,subnode3 .Now when the user click on its any subnode then the new subtree will be genrated with the help of create subtree class . The class refresh remove all the child node of the subtree .

The some portion of the code of the tree is as

public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)allServer.getLastSelectedPathComponent();
if (node == null) return;
Object nodeInfo = node.getUserObject();
//if((0 != (node.getChildCount())) || -1 !=(node.getChildCount()))
//getChildren((DefaultMutableTreeNode )nodeInfo);
//if(false!=getChildren((DefaultMutableTreeNode )nodeInfo).isEmpty())
refreshTree((DefaultMutableTreeNode )nodeInfo);

Vector server=null;
if (nodeInfo.equals("subnode1")) {
str ="a";
server = ServerName(str);
CreateSubTree(node,server,str);
}
else if(nodeInfo.equals("subnode2")) {
str ="b";
server = ServerName(str);
CreateSubTree(node,server,str);
}
else if(nodeInfo.equals("subnode3")) {
str ="c";
server = ServerName(str);
CreateSubTree(node,server,str );
}
else if(DEBUG) { //!= node.isLeaf()) {
// this.st.setVisible(true);
}
}

public void refreshTree(DefaultMutableTreeNode node)
{
DefaultMutableTreeNode rootNode = getRootNode(node);
if(rootNode != null)
System.out.println("The value of the childCount is" +rootNode);
int childCount = rootNode.getChildCount();
System.out.println("The value of the childCount is" +childCount);
if(childCount > 0)
{
rootNode.removeAllChildren();
DefaultTreeModel defaultTreeModel = (DefaultTreeModel)allServer.getModel();
if(defaultTreeModel != null)
{

defaultTreeModel = (DefaultTreeModel)allServer.getModel();

defaultTreeModel.nodeStructureChanged(rootNode);

defaultTreeModel.reload(rootNode);

// this two lines of code helps in expanding the tree when a node is added to the tree
TreePath selectedPath = new TreePath(defaultTreeModel.getPathToRoot(rootNode));
allServer.expandPath(selectedPath);

}
}
}


public DefaultMutableTreeNode getRootNode(DefaultMutableTreeNode node1)
{
DefaultTreeModel defaultTreeModel = (DefaultTreeModel)allServer.getModel();
DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode)defaultTreeModel.getChild((DefaultMutableTreeNode)defaultTreeModel.getRoot(),node1.getLevel());//.getRoot();

return rootNode;
}

public void CreateSubTree(DefaultMutableTreeNode node ,Vector server,String str){

Enumeration en=server.elements();
while(en.hasMoreElements())
{
DefaultMutableTreeNode mynode = new DefaultMutableTreeNode(en.nextElement());
node.add(mynode);
}

}

what is wrong please help

Thanks in advance,
anum
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 12 2005
Added on Sep 13 2005
3 comments
125 views