Import Java. AWT. borderlayout; import Java. AWT. component; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. event. keyevent; import Java. AWT. event. keylistener; import Java. io. file; import Java. util. vector; import javax. swing. jbutton; import javax. swing. jcombobox; import javax. swing. jframe; import javax. swing. jpanel; import javax. swing. jscrollpane; import javax. swing. jtree; Im Port javax. swing. event. treeexpansionevent; import javax. swing. event. treeselectionevent; import javax. swing. event. treeselectionlistener; import javax. swing. event. treewillexpandlistener; import javax. swing. tree. defaultmutabletreenode; import javax. swing. tree. defaulttreemodel; import javax. swing. tree. expandvetoexception; import javax. swing. tree. treenode; import javax. swing. tree. treepath; public class filetree {Jframe JF = new jframe ("file tree"); jcombobox combo; jtree tree; defaultmutabletreenode root; defaulttreemodel model; Public void Init () {vector <string> historypath = new vector <string> (); historypath. add ("C :\\"); historypath. add ("d :\\"); historypath. add ("E: \"); historypath. add ("f :\\"); jpanel = new jpanel (); combo = new jcombobox (historypath); combo. seteditable (true); component [] components = combo. getcom Ponents (); system. out. println (components. length); For (INT I = 0; I <components. length; I ++) {components [I]. addkeylistener (New keylistener () {public void keypressed (keyevent e) {} public void keyreleased (keyevent e) {If (E. getkeycode () = keyevent. vk_enter) {string Path = combo. geteditor (). getitem (). tostring (); redrawtree (PATH) ;}} public void keytyped (keyevent e) {}});} panel. setlayout (New borderlayout () ); Panel. add (Combo); jbutton BTN = new jbutton (">"); BTN. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {string Path = combo. getselecteditem (). tostring (); redrawtree (PATH) ;}}); panel. add (BTN, borderlayout. east); JF. add (panel, borderlayout. north); inittree (); Model = (defaulttreemodel) tree. getModel (); JF. add (New jscrollpane (tree); JF. setdefaclocloseoperation (jframe. exit _ On_close); JF. pack (); JF. setsize (600,400); JF. setvisible (true);}/*** initialization */private void inittree () {root = new defaultmutabletreenode ("computer"); file [] file = file. listroots (); For (file F: File) {defaultmutabletreenode disknode = new defaultmutabletreenode (F. getabsolutepath (); disknode. add (New defaultmutabletreenode (null); root. add (disknode);} tree = new jtree (Root); tree. addtreewillexpandlistener (New Treewillexpandlistener () {public void treewillcollapse (treeexpansionevent) throws expandvetoexception {} public void treewillexpand (treeexpansionevent event) throws expandvetoexception {treepath Path = event. getpath (); expandtree (PATH) ;}}); tree. addtreeselectionlistener (New treeselectionlistener () {public void valuechanged (treeselectionevent e) {treepath Path = E. getnewleadselectionpath (); expan Dtree (PATH) ;}}) ;}/ *** generate the directory * @ Param path */private void redrawtree (string path) based on the input path) {/* method 1 is automatically generated */defaultmutabletreenode lastnode = showalldirect ("", root, PATH); treenode [] nodes = model. getpathtoroot (lastnode); treepath TP = new treepath (nodes); tree. scrollpathtovisible (TP); tree. updateui ();/* method 2 uses the treewillexpandlistener of the tree to call the corresponding method for generation (see the Comment Method 'showalldirect (...) ') * // showalldirect (path, "", root );} // Private void showalldirect (string path, string pre, defaultmutabletreenode parent) {// defaultmutabletreenode selectednode = NULL; // pre = pre + (pre. endswith ("\") | pre. equals ("")? "": "\"); // For (INT I = 0; I <parent. getchildcount (); I ++) {// defaultmutabletreenode childnode = (defaultmutabletreenode) parent. getchildat (I); // If (path. startswith (pre + childnode. tostring () {// selectednode = childnode; // break; //} // If (selectednode = NULL | selectednode. getchildcount () = 0) // return; // defaultmutabletreenode newnode = (defaultmutabletreenode) selectednode. getchildat (0 );/ /Treenode [] nodes = model. getpathtoroot (newnode); // treepath TP = new treepath (nodes); // tree. scrollpathtovisible (TP); // tree. updateui (); // If (! Path. equals (selectednode. tostring () {// showalldirect (path, pre + selectednode. tostring (), selectednode ); ///} //}/*** directory generated by path * @ Param file * @ Param root * @ Param path * @ return directory bottom node */private defaultmutabletreenode showalldirect (string filename, defaultmutabletreenode root, string path) {defaultmutabletreenode returnnode = root;/* generated sub-directory **/If (root. getchildcount ()> 0 & (defaultmutabletre Enode) root. getchildat (0). getuserobject ()! = NULL) {string name = filename; For (INT I = 0; I <root. getchildcount (); I ++) {defaultmutabletreenode childnode = (defaultmutabletreenode) root. getchildat (I); name = filename + (filename. equals ("") | filename. endswith ("\\")? "": "\") + Childnode; If (path. startswith (name) {returnnode = showalldirect (name, childnode, PATH); break;}/* Find the next level node of the given path */else if (name. startswith (PATH) {returnnode = childnode; break ;}}/ * new sub-directory */else {file [] files = new file (filename ). listfiles (); If (Files = NULL) return root; root. removeallchildren (); For (file F: Files) {defaultmutabletreenode childnode = new defaultmutabletreenode (F. get Name (); If (F. isdirectory () childnode. add (New defaultmutabletreenode (null); root. add (childnode); If (path. startswith (F. getabsolutepath () {returnnode = showalldirect (F. getabsolutepath (), childnode, PATH);}/* Find the next level node of the given path */else if (F. getabsolutepath (). startswith (PATH) {returnnode = childnode; }}return returnnode;}/*** click to generate a sub-directory * @ Param path */private void expandtree (treepath path) {If (Path = Null) return; string filename = ""; for (INT I = 1; I <path. getpathcount (); I ++) {string STR = path. getpathcomponent (I ). tostring (); filename + = (STR + (Str. endswith ("\\")? "": "\");} Combo. removeitemat (0); combo. insertitemat (filename, 0); combo. setselectedindex (0); defaultmutabletreenode selnode = (defaultmutabletreenode) path. getlastpathcomponent ();/* sub-directory generated **/If (selnode. getchildcount ()> 0 & (defaultmutabletreenode) selnode. getchildat (0 )). getuserobject ()! = NULL) {return;} If (selnode. isleaf () {} else {file [] files = new file (filename ). listfiles (); If (Files = NULL) return; selnode. removeallchildren (); For (file F: Files) {defaultmutabletreenode childnode = new defaultmutabletreenode (F. getname (); If (F. isdirectory () childnode. add (New defaultmutabletreenode (null); selnode. add (childnode) ;}} public static void main (string [] ARGs) {New filetree (). init ();}}