1

I have following code for create directory structure in Java. code is working fine
But I have some doubt
For directory structure that I have check expanded node is directory or file If it is directory then traverse it and add File(s) & sub-Directories of that parent directory.
I am doing it every time when user expand node. It is ok when directory is first time traverse and add node to It.
But when it expand second time then code will add content same as first expand ans so on...
But JTree contain node(s) which is same in directory as per coding it should contain (Directory content * numbers of time that node expand )
How this is maintain I am adding every time when node expand and I am getting node as First time expand.
I am making any Mistake?
Code :

import java.io.*;    
import javax.swing.*;    
import javax.swing.tree.*;    
import java.awt.*;    
import java.awt.event.*;    
import java.util.*;    
import javax.swing.event.*;    
public class tree_drive    
{    
    JFrame frm;    
    JFileChooser file_c;    
    JTree f_tree;    
    public tree_drive(String path)    
    {    
        frm=new JFrame("Jtree Demo");       
        Container cp=frm.getContentPane();      
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
        DefaultMutableTreeNode top = new DefaultMutableTreeNode(path);    
        f_tree=new JTree(top);    
        JScrollPane treeView = new JScrollPane(f_tree);    
        f_tree.addTreeSelectionListener(new TreeSelectionListener()    
        {    
            public void valueChanged(TreeSelectionEvent e)    
            {    
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)                                              f_tree.getLastSelectedPathComponent();    
                frm.setTitle("You hava selected : "+ node.toString());    
                    if (node != null)    
                {
                    File t_file=new File(node.toString());    
                    if(t_file.isDirectory())    
                    {    
                        try    
                        {    
                            for(File f : t_file.listFiles()) 
                            {    
                                if(f.isDirectory())   
                                {
    node.add(new DefaultMutableTreeNode(f.getPath()+File.separatorChar,true));  
                                }    
                                else    
                                {    
                node.add(new    DefaultMutableTreeNode(f.getName(),false));     
                                }    
                            }       

                        }    
                        catch(Exception ge)    
                        {   System.out.println(e);              

                        }                   
                    }    
                }    
            }    
        });    
        frm.add(treeView);    
        frm.setSize(500,500);    
        frm.setVisible(true);    
        frm.pack();         
    }    
    public static void main(String ...t)    
    {       
        try    
        {    
            new tree_drive(t[0]);           
        }    
        catch(Exception e)    
        {    
            System.out.println("Plese Specify path from Command line");    
        }           
    }    
}

Output :

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mahesh Meniya
  • 2,627
  • 3
  • 18
  • 17
  • 3
    *"I am making any Mistake?"* Not sure, but have a look at [File Browser GUI](http://codereview.stackexchange.com/questions/4446/file-browser-gui) it seems much the same as what you are trying to do. – Andrew Thompson Nov 12 '12 at 14:16
  • code is working But I am adding node when node is expand this process is done every time So should append the content of that node(if it is directory) But it is not appending and I am not getting the reason. – Mahesh Meniya Nov 12 '12 at 14:25
  • @AndrewThompson :Thanks for Interest & ur Time... – Mahesh Meniya Nov 12 '12 at 14:27
  • 1
    See also the [alternative cited here](http://stackoverflow.com/a/13258638/230513). – trashgod Nov 12 '12 at 15:03

0 Answers0