Java implementation file tree

Source: Internet
Author: User

You want your swingProgramDo you have a tree like a resource manager? Do I need lazy loading?

Let's look at the file tree I implemented below.Code

Public class assumertree extends jpanel {<br/> private jtree tree; <br/> private treepath; </P> <p> Public assumertree () {<br/> This. setlayout (New borderlayout (); <br/> defaultmutabletreenode root = new defaultmutabletreenode ("Explorer"); <br/> tree = new jtree (Root ); <br/> file [] Roots = file. listroots (); <br/> for (INT I = 0; I <roots. length; I ++) {<br/> defaultmutabletreenode Node = New defaultmutabletreenode (roots [I]. getpath (); <br/> root. add (node); <br/>}< br/> mouselistener ML = new mouseadapter () {<br/> Public void mousepressed (mouseevent E) {<br/> try {<br/> treepath TP = tree. getpathforlocation (E. getx (), E. gety (); <br/> If (TP! = NULL) {<br/> treepath = TP; <br/> int size = treepath. getpath (). length; <br/> stringbuffer pathname = new stringbuffer (); <br/> If (size> 0) {<br/> for (INT I = 1; I <size; I ++) {<br/> if (I> 2) {<br/> pathname. append ("//"); <br/>}< br/> pathname. append (treepath. getpath () [I]. tostring (); </P> <p >}< br/> system. out. println (pathname. tostring (); <br/> file = new file (pathname. tostring (); <br/> file [] list = file. listfiles (); <br/> for (INT I = 0; I <list. length; I ++) {<br/> string [] strings = list [I]. getpath (). split ("//"); <br/> defaultmutabletreenode node = new defaultmutabletreenode (strings [Strings. length-1]); <br/> (defaultmutabletreenode) TP. getlastpathcomponent ()). add (node); <br/>}< br/>}catch (exception E1) {<br/> system. out. println ("can not open"); <br/>}< br/>}; <br/> tree. addmouselistener (ML); <br/> tree. setshowsroothandles (true); <br/> tree. setrootvisible (true); <br/> jscrollpane scrpane = new jscrollpane (tree); <br/> Add (scrpane ); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> jframe = new jframe ("Explorer "); <br/> jframe. setlayout (New borderlayout (); <br/> jframe. add (New assumertree (); <br/> jframe. setdefaclocloseoperation (jframe. exit_on_close); <br/> jframe. setsize (300,700); <br/> jframe. setvisible (true); <br/>}< br/>}

I have been busy recently. If I don't have time to write a blog, I will use some code to show my ugliness.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.