• Post Reply Bookmark Topic Watch Topic
  • New Topic

Display using Tree  RSS feed

 
Huang Crystal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have already implemented a Tree at the left panel of my GUI. But I want it to display the file (or all files in that particular directory) on the other panel, when clicked by the user, just like the function of the window explorer. How should I go about doing it?
Below are parts of my code:
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
getTree(root,new File("c:\\jakarta-tomcat\\trans\\data"));
JTree jTree2 = new JTree(root);
jPanel2.add(jTree2);
jTree2.putClientProperty("JTree.lineStyle", "Angled");
private void getTree(DefaultMutableTreeNode parent, File f)
{
String fileNames[] = f.list();
for(int i=0;i<fileNames.length;i++)>
{ //files within the directory
DefaultMutableTreeNode child = new DefaultMutableTreeNode(fileNames[i]);
parent.add(child);
File fTmp = new File(f,fileNames[i]);
if(fTmp.isDirectory())
getTree(child,fTmp);
}
Thanx so much..
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Huang,
Start here:
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
Enjoy,
Manfred.
 
sihem merah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I implemented a method that constructs a tree from a root node and a directory.
Here is the code. I hope it will helps you.
public void constructTree(DefaultMutableTreeNode currentNode, File currentFile)
{
File[] lf = currentFile.listFiles();
for (int i=0; i < lf.length ; i++)
{
DefaultMutableTreeNode node = new DefaultMutableTreeNode(lf[i].getName(),!lf[i].isFile());
currentNode.add(node);
if (lf[i].isDirectory())
{
constructTree(node,lf[i]);
}
}
}
Ciao!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!