The problem I am facing is with the tree model of the JTree.
I have defined the root node as:
javax.swing.tree.DefaultMutableTreeNode rootNode = new javax.swing.tree.DefaultMutableTreeNode(projectName);
When the application first starts, I want the treeModel to be created and loaded. For this, I am using a file meta.txt, which has information like the following:
1QuotesPrice.Job
2QuotesPrice.Df
1Quotes.Job
2Quotes.Wf
3Quotes.Df
2Falkeblang.Wf
3Falkeblang.Df
The first column is the level, and the second is the node of the tree. Now based on this information, I want to create the tree model, but am facing a roadblock here.
I am unsure of the logic to apply to add the above nodes to the root node. Every thing I have tried seems to have flaws. The problem here is the no of levels are not fixed, so I cant simply use a if-else construct. I am using the following code:
String treeMeta=this.projectsDir+"\\"+projectName+"\\"+"meta.txt";
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(projectName);
File f1=new File(treeMeta);
inputStream = new Scanner(f1);
while(inputStream.hasNext()){
String val=inputStream.next();
System.out.println("!@#$%"+val+"!@#$%");
treeNodePrev=treeNode;
prevLevel=level;
level=val.substring(0,1);
nodeVal=val.substring(1);
if(level.equals("1")){
prevNode=projectName;
treeNode = new DefaultMutableTreeNode(nodeVal);
System.out.println("added to root node");
rootNode.add(treeNode);
//System.out.println("added to root node");
}else if(Integer.parseInt(level)>Integer.parseInt(prevLevel)){
prevNode=nodeVal;
treeNode = new DefaultMutableTreeNode(nodeVal);
treeNodePrev.add(treeNode);
}else if(Integer.parseInt(level)==Integer.parseInt(prevLevel)){
prevNode=nodeVal;
treeNode = new DefaultMutableTreeNode(nodeVal);
prevParentTreeNode=(DefaultMutableTreeNode)treeNodePrev.getParent();
//System.out.println(prevParentTreeNode.getParent().toString());
prevParentTreeNode.add(treeNode);
}
}
jProjectTree.setModel(new javax.swing.tree.DefaultTreeModel(rootNode));
jScrollPane2.setViewportView(jProjectTree);
This seems to be working fine, but could anyone please tell me if this is the correct approach?
This might be a bit cleaner. Just adapt the function for calculating the level ...
There is a github project for creating TreeModels from a Collection of data if that helps.
Example below on building a TreeModel from a List of Book Objects.
Assumes Book is in same package and looks like...
For more info:
https://github.com/mnrussell/collectionTreeModel
Bit late but hope this helps :o)