Swing Show System file tree

Source: Internet
Author: User

In general, the construction of a SWING tree, is the first to build a good data storage model (TreeModel), in general, the implementation of the problem, but when the amount of data is very large, one-time build good TreeModel, will spend a lot of time, interface In the gray State (of course you can open other threads, you can operate other interfaces), this gives the user a very poor feeling. The key to solving this problem is the data of the tree model. As we all know, system files are layered and layered, so we can be a file (whether file or folder) as a tree node, when the first load tree, click on the tree node, first to determine whether it has child nodes, if not, to read the node's saved file information, if the folder, To take all the files in its folder, construct the tree node and add it to the tree. The second time you click, you won't be able to read the disk file system.

Code listings:

import Javax.swing.tree.DefaultMutableTreeNode;


import Javax.swing.tree.DefaultTreeModel;


import java.io.*;


import java.awt.*;


import javax.swing.*;


import Javax.swing.tree.TreeCellRenderer;


import Java.awt.event.MouseAdapter;


import java.awt.event.MouseEvent;


import Javax.swing.tree.TreePath;


/**


* <p>Title:JDHSystemFileTree</p>


* <p>description: System directory tree, dynamic generation, solve load slow problem </p>


* <p>copyright:copyright (c) 2007</p>


* @author Chiang's family frenzy


* @version 1.0


*/


public class Systemfiletree {


private Defaulttreemodel model;


private JTree tree;


public Systemfiletree () {


JFrame f=new JFrame ();


F.getcontentpane (). setlayout (New BorderLayout ());


tree=new JTree ();


Tree.addmouselistener (New Mouseadapter () {


public void mousepressed (MouseEvent e) {


node_mouseaction (e);


       }


     });


jscrollpane scroll=new JScrollPane (tree);


F.getcontentpane (). Add (Scroll,borderlayout.center);


f.setlocation (250,250);


f.setsize (New Dimension (300,500));


f.setvisible (TRUE);


   }


private void Node_mouseaction (MouseEvent e) {


int row = Tree.getrowforlocation (E.getx (), e.gety ());


Pathnode Pathnode =null;


if (row!=-1) {


TreePath Path = Tree.getpathforrow (row);


Pathnode = (pathnode) path.getlastpathcomponent ();


if (Pathnode.isfolder () &&pathnode.getchildcount () ==0) {


Buildernode (Pathnode);


Tree.expandpath (path);


     }


     }


   }


private Pathnode Buildernode (Pathnode pathnode) {


String filepath= pathnode.getvalue (). toString ();


file File=new file (FilePath);


file[] Files=file.listfiles ();


for (int i=0;i<files.length;i++) {


pathnode node=new Pathnode (Files[i].getname (), Files[i].getabsolutepath (), files[i].isdirectory ());


pathnode.add (node);


}


return pathnode;


   }


private void InitData (String rootpath) {


file F=new file (RootPath);


pathnode root=new Pathnode (F.getname (), rootpath,f.isdirectory ());


file[] Files=f.listfiles ();


for (int i=0;i<files.length;i++) {


pathnode node=new Pathnode (Files[i].getname (), Files[i].getabsolutepath (), files[i].isdirectory ());


root.add (node);


     }


model=new Defaulttreemodel (root);


Tree.setmodel (model);


filetreerenderer renderer=new filetreerenderer ();


tree.setcellrenderer (renderer);


Tree.repaint ();


}


class Filetreerenderer implements treecellrenderer{


Private Icon folder_open=new ImageIcon ("icons/folder_open.jpg");


Private Icon folder_close=new ImageIcon ("icons/folder_close.jpg");


Private Icon file=new ImageIcon ("Icons/file.gif");


public Component gettreecellrenderercomponent (jtree tree, Object value,


Boolean selected, Boolean expanded, Boolean leaf, int row,


Boolean hasfocus) {


JLabel label = NULL;


if (value!= null) {


System.out.println (Value.getclass (). toString ());


if (value instanceof Pathnode) {


Pathnode Pathnode = (pathnode) value;


if (Pathnode.isfolder ()) {


if (expanded) {


label = new JLabel (Pathnode.getuserobject ().


toString (),


Folder_open, jlabel.right);


else if (!expanded| | Leaf) {


label = new JLabel (Pathnode.getuserobject ().


toString (),


Folder_close, jlabel.right);


             }


} else {


label = new JLabel (Pathnode.getuserobject (). toString (),


file, jlabel.right);


           }


return label;


         }


      }


return label;


     }


}


class Pathnode extends defaultmutabletreenode{


Object value;


Boolean isfolder;


public Pathnode (String name,object value,boolean isfolder) {


super (name);


This.value=value;


This.isfolder=isfolder;


}


public Object GetValue () {


return value;


     }


public boolean Isfolder () {


return isfolder;


     }


   }


public static void Main (String args[]) {


jdhsystemfiletree tree=new jdhsystemfiletree ();


//For a path demo


tree.initdata ("d:/");


   }


}

Of course, you can further achieve the ease of use of the interface, that is, after the initial click, after loading, let the mouse into the waiting shape.

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.