Well I am currently designing a JTree that is dynamicly loaded from a given database.
The way I did it is that I created a class that stores an id, a parent node, a list of child nodes, and the data. Then what I did was creating a list of this class from the original data and did this thing below.
private void writeRTTree()
{
//Change the root node display.
//Initialise the initial nodes.
rtTreeNodes = new DefaultMutableTreeNode[rtNodesFile.nLines];
for(int index = 0; index < rtNodesFile.nLines; index++)
{
//Create a node has the name of it's corresponding report type string.
rtTreeNodes[index] = new DefaultMutableTreeNode(rtNodesFile.rtNodes[index].rtData);
//Insert the node to it's appropriate place.
if(rtNodesFile.rtNodes[index].rtParent == 0)
{
//Adds the node to the root.
rtTreeModel.insertNodeInto(rtTreeNodes[index], rtTreeRoot, rtTreeRoot.getChildCount());
}
else
{
//Adds the node to the corresponding parent.
rtTreeModel.insertNodeInto(rtTreeNodes[index], rtTreeNodes[rtNodesFile.rtNodes[index].rtParent - 1], rtTreeNodes[rtNodesFile.rtNodes[index].rtParent - 1].getChildCount());
}
}
}
The loading was sucessful but when I was trying to locate the user selected node by using "getSelectionPath()", I failed and whan i was trying to print it it returns null pointer exception. However this function works fine before I load the tree.
I sorta knows that my problem is probably because those JTree nodes are created in a tree, but if I dont do that is there any ways I can create those nodes and make them able to respond to user's selection? Or is there any ways I do not need to correct my "writeRTTree()" function and that would be so much better.
Thank You