/Users/lyon/j4p/src/gui/tree/FileTreeTest.java

1    package gui.tree; 
2     
3    import futils.Futil; 
4     
5    import javax.swing.*; 
6    import javax.swing.event.TreeSelectionEvent; 
7    import javax.swing.event.TreeSelectionListener; 
8    import javax.swing.tree.TreePath; 
9    import java.awt.event.WindowAdapter; 
10   import java.awt.event.WindowEvent; 
11   import java.io.File; 
12   import java.io.FileNotFoundException; 
13    
14   public class FileTreeTest { 
15       public static void main(String[] args) { 
16           try { 
17    
18               JFrame f = new JFrame("File Tree Test"); 
19               File dir = Futil.getReadDirFile("select file"); 
20               classUtils.putils.ClassPathUtils.addClassPath(dir + ""); 
21    
22               JScrollPane jsp = getJscrollTree(dir); 
23    
24    
25               f.getContentPane().add(jsp); 
26               f.setSize(300, 300); 
27               f.addWindowListener(new WindowAdapter() { 
28                   public void windowClosing(WindowEvent evt) { 
29                       System.exit(0); 
30                   } 
31               }); 
32               f.setVisible(true); 
33           } catch (FileNotFoundException e) { 
34               System.out.println("File " + args[0] + " not found"); 
35           } 
36       } 
37    
38       private static JScrollPane getJscrollTree( 
39               File dir) 
40               throws FileNotFoundException { 
41           final FileTree ft = new FileTree(dir + ""); 
42    
43    
44           ft.addTreeSelectionListener(new TreeSelectionListener() { 
45               public void valueChanged(TreeSelectionEvent evt) { 
46                   TreePath path = evt.getPath(); 
47                   String name = ft.getPathName(path); 
48                   System.out.println(evt); 
49                   System.out.println("File " + name + " has been " 
50                           + (evt.isAddedPath() ? "selected" : "deselected")); 
51               } 
52           }); 
53           ft.getComponents(); 
54    
55           JScrollPane jsp = new JScrollPane(ft); 
56           return jsp; 
57       } 
58   } 
59