Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

jtree problem

How do I delete a node from a treemodel? I tried 2 delete one using treeModel.removeNodeFromParent(temp_node); but my temp_node does not contain a parent in it. This is because it was defined on its own has a DefaultMutableTreeNode temp_node; where I wanted to create a loop that gets each node from a parent and temporarily insert the object into the temp_node..

//get length of each online and offline nodes.
int online_count = treeModel.getChildCount(online) - 1;
int offline_count = treeModel.getChildCount(offline) - 1;

Object temp;

for(int i =0; i <= online_count; i++)
{
//get the username
temp = treeModel.getChild(online, i);
JOptionPane temp_node = new DefaultMutableTreeNode(temp);

//convert it to string
String info = temp_node.toString();

String status = query(info);
// if their offline, remove from online node and insert
//into the offline node.


JOptionPane.showMessageDialog(null, " status of " + info + " is" + status, null, JOptionPane.ERROR_MESSAGE);

if(status.equals("Offline") == true)
{
treeModel.removeNodeFromParent(temp_node);
treeModel.insertNodeInto(temp_node, offline, offline.getChildCount());

//repaint the jtree
userTree.setModel(treeModel);
userTree.scrollPathToVisible(new TreePath(offline.getPath()));

JOptionPane.showMessageDialog(null, "node moved succesfuly in offline,", null, JOptionPane.ERROR_MESSAGE);
}




}

Where it gets the username i.e. temp = treeModel.getChild(online, i); how can I get both the parent and object of that specific node so that I can remove the node from its treemodel. Any comments?
[2483 byte] By [jonneymendoza] at [2007-11-11 8:35:38]