Package package2; import javax. swing. *; import javax. swing. event. *; import javax. swing. tree. *; import Java. AWT. *; import Java. AWT. event. *; import Java. io. *; import Java. util. *;/** tree exercise, default node defaultmutabletreenode **/public class tree_practice extends jframe {container; jtree; jscrollpane scrollpane; Public tree_practice () {This. setdefaclocloseoperation (jframe. exit_on_close); this. container = T His. getcontentpane (); Response root = new defaultmutabletreenode ("C: \"); Response col1 = new response ("Docs"); defaultmutabletreenode col2 = new defaultmutabletreenode ("RENAME "); root. add (col1); root. add (col2); defaultmutabletreenode col21 = new defaultmutabletreenode ("API"); defaultmutabletreenode col22 = new defaultmutabletreenode ("index. hlml ", false); col1.add (Col21); col1.add (col22); defaultmutabletreenode col31 = new defaultmutabletreenode ("swing. tree. * "); col21.add (col31); defaultmutabletreenode col41 = new defaultmutabletreenode (" jcompnent.html ", false); defaultmutabletreenode col42 = new release (" jbutton. hlml ", false); defaultmutabletreenode col43 = new defaultmutabletreenode (" jlabel. hlml ", false); col31.add (col43); col31.add (col42); col31.ad D (col41); tree = new jtree (Root); scrollpane = new jscrollpane (tree); this. container. add (scrollpane); this. setlocation (300,100); this. setsize (600,600); this. setvisible (true) ;}} class ttreecellrenderer {public ttreecellrenderer () {tree_practice T = new tree_practice (); // T. tree. putclientproperty ("jtree. linestyle "," angled "); // T. tree. putclientproperty ("jtree. linestyle "," NONE "); defaulttreecellrenderer render = (Defaulttreecellrenderer) T. tree. getcellrenderer (); // sets the display style T. tree. setrowheight (40); render. setdoublebuffered (true); render. setleaficon (New imageicon ("7.png"); // render of the leaf icon. setopenicon (New imageicon ("10.png"); // click the render icon to open the node. setclosedicon (New imageicon ("9.png"); // node close icon render. setfont (new font ("monospaced", Font. bold | font. italic, 15); render. settextnonselectioncolor (color. black); // if not selected, the font color render. settext Selectioncolor (color. red); // The Font Color render when selected. setborderselectioncolor (color. blue); // The border color T when selected. invalidate () ;}}/** custom tree model: displays the jtree of the file system hierarchy */class ttreemodel extends jframe {public ttreemodel () {super ("ttreemodel "); filesystemmodel FS = new filesystemmodel (); jtree tree = new jtree (FS); jscrollpane scroll = new jscrollpane (tree); this. getcontentpane (). add (scroll); this. setsize (600,600); this. setdefaclocloseope Ration (jframe. exit_on_close); this. setvisible (true) ;}} class filesystemmodel implements treemodel, treemodellistener {private string root; private vector listeners; Public filesystemmodel () {// This. root = system. getproperty ("user. home "); // If (system. getproperty ("OS. name "). equals ("Windows 95") {// file tempfile = new file (Root); // root = tempfile. getparent (); //} This. root = "d :\\"; this. listeners = new vecto R () ;}@ overridepublic object getroot () {return (new file (Root) ;}@ overridepublic object getchild (Object parent, int index) {file directory = (file) parent; string [] dirmembers = directory. list (); Return (new file (directory, dirmembers [Index]) ;}@ overridepublic int getchildcount (Object parent) {file = (File) parent; If (file. isdirectory () {string [] dirmembers = file. list (); If (dirmembers = NULL) Return 0; return dirmembers. length;} elsereturn 0;} @ overridepublic Boolean isleaf (Object node) {return (File) node ). isfile () ;}@ overridepublic void valueforpathchanged (treepath path, object newvalue) {// todo auto-generated method stub} @ overridepublic int getindexofchild (Object parent, object child) {file directory = (File) parent; string [] dirmembers = directory. list (); file fchild = (File) child; fo R (Int J = 0; j <dirmembers. length; j ++) if (fchild. getname (). equals (dirmembers [J]) return J; Return-1 ;}@ overridepublic void addtreemodellistener (treemodellistener L) {If (L! = NULL &&! This. listeners. Contains (L) {This. listeners. Add (l) ;}@overridepublic void removetreemodellistener (treemodellistener L) {If (L! = NULL) {This. listeners. removeelement (l) ;}@ overridepublic void initialize (treemodelevent e) {// todo auto-generated method stub} @ overridepublic void treenodesinserted (treemodelevent e) {enumeration listenercount = This. listeners. elements (); While (listenercount. hasmoreelements () {treemodellistener listener = (treemodellistener) listenercount. nextelement (); listener. treenodesinserted (E); }}@ overridepublic void treenodesremoved (treemodelevent e) {// todo auto-generated method stub} @ overridepublic void treestructurechanged (treeevent modele) {// todo auto-generated method stub} // specifies the class ttreeselectionevent extends jframe {container; jsonroot; jscrollpane treepane; jscrollpane editorpane; jeditorpane editor; ttreeselectionevent temp = This; Public ttreeselectionevent () {super ("ttreeselectionevent"); this. container = This. getcontentpane (); this. createtreenodes (); jtree tree = new jtree (Root); // tree. getselectionmodel (). setselectionmode (// treeselectionmodel. single_tree_selection); tree. addtreeselectionlistener (New treeselectionlistener () {private void dosingleselection (treeselectionevent e) {jtree tree = (jtree) E. getsource (); defaultmutabletreenode select = (defaultmutabletreenode) tree. getlastselectedpathcomponent (); treepath [] TP = tree. getselectionpaths (); If (select. isleaf () {treepath Path = E. getpath (); object [] nodes = path. getpath (); string S = ""; for (Int J = 0; j <nodes. length; j ++) s ++ = (defaultmutabletreenode) nodes [J]). tostring () + "\\\\"; Editor. seteditable (true); s = S. substring (0, S. length ()-2); Editor. settext (s); Editor. addfocuslistener (New focuslistener () {string file; @ overridepublic void focusgained (focusevent e) {file = editor. gettext () ;}@ overridepublic void focuslost (focusevent e) {try {If (file = NULL) return; file F = new file (File); F. createnewfile (); filewriter fw = new filewriter (File); FW. write (editor. gettext (); FW. close () ;} catch (ioexception E1) {// todo auto-generated catch blocke1.printstacktrace () ;}}}}@ overridepublic void valuechanged (treeselectionevent E) {jtree tree = (jtree) E. getsource (); treeselectionmodel Ts = tree. getselectionmodel (); If (TS. getselectionmode () = treeselectionmodel. single_tree_selection) {// select this. dosingleselection (E);} else if (TS. getselectionmode () = treeselectionmodel. contiguous_tree_selection) {// consecutive selection} else {// multiple selection treepath [] TP = tree. getselectionpaths (); If (TP = NULL) return; temp. editor. addfocuslistener (null); temp. editor. seteditable (false); string Path = "All path: \ n"; for (Int J = 0; j <TP. length; j ++) path + = TP [J]. tostring () + "\ n"; temp. editor. settext (PATH) ;}}); this. treepane = new jscrollpane (tree); this. editor = new jeditorpane (); this. editorpane = new jscrollpane (this. editor); jsplitpane splitpane = new jsplitpane (jsplitpane. horizontal_split, true, this. treepane, this. editorpane); this. container. add (splitpane); this. setdefaclocloseoperation (jframe. exit_on_close); this. setsize (600,600); this. setvisible (true);} private void createtreenodes () {This. root = new topology ("D:"); defaultmutabletreenode col1 = new defaultmutabletreenode ("data"); defaultmutabletreenode col2 = new defaultmutabletreenode ("Swing"); root. add (col1); root. add (col2); defaultmutabletreenode col11 = new defaultmutabletreenode ("first.txt"); export col12 = new partition ("second.txt"); col1.add (col11); col1.add (col12 );}}
Recently I need to use swing to compile something. I learned how to use jtree. The specific code is as follows: