jTree to create hard drive file directory tree

2010-06-13  来源:本站原创  分类:Java  人气:146 

Option One: the hard drive using recursive traversal file, and add child nodes

Possible degrees: the end of reasons: because we traverse the entire hard disk, the speed is very slow

Option Two: Expand the node to dynamically add a child node

Possible degrees: High Reason: to make up for deficiencies in the program a

Program a key code:

private static final String []_root = {"A:","B:","C:","D:","E:","F:","G:",
                                          "H:","I:","J:","K:","L:","M:","N:",
                                          "O:","P:","Q:","R:","S:","T:","U:",
                                          "V:","W:","X:","Y:","Z:"};
DefaultMutableTreeNode top = new DefaultMutableTreeNode("My Computer..."); ;
for(int i=0;i<_root.length;i++){
     File f = new File(_root[i]);
     if(f.isDirectory()){
           DefaultMutableTreeNode tempnode = new DefaultMutableTreeNode(_root[i]);
           top.add(tempnode);
           traversalNode(tempnode);  // Walk through the add a child node
     }
}
jTree1 = new JTree(top);
public void traversalNode(DefaultMutableTreeNode dmtnf){
        DefaultMutableTreeNode tempNode_p = dmtnf;
        DefaultMutableTreeNode tempNode_c;
        File ff = new File(f.toString());
        for(int i=0;i<ff.listFiles().length;i++){
            if(ff.listFiles()[i].isDirectory()){
                DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(ff.listFiles()[i].toString());
                tempNode_c = traversalNode(childNode);
                tempNode_p.add(tempNode_c);
            }else{
                tempNode_c = new DefaultMutableTreeNode(ff.listFiles()[i]);
                tempNode_p.add(tempNode_c);
            }
        }
}

Option Two key code:

Option II added context menu for sorting and (when going to start when the call traversalNode)

public static final String TEMP_FILE_NAME = "   ###!!!";// The menu expands to add a child node so that it can be expanded  

 public void traversalNode(DefaultMutableTreeNode parentNode){
        DefaultMutableTreeNode tempNode_p = parentNode;
        DefaultMutableTreeNode tempNode_c;

        if(tempNode_p.getChildCount()==0){
            return ;
        }
        if(tempNode_p.getFirstChild().toString().equals(Traversal.TEMP_FILE_NAME)){
            tempNode_p.removeAllChildren();
            File parentf = new File(tempNode_p.toString());
            File[] filelist = parentf.listFiles();
            File[] fileDirectory = new File[filelist.length];
            File[] file = new File[filelist.length];
            int num_Directory = 0;
            int temp_d = 0;
            int temp_f = 0;
            for (int i = 0; i < filelist.length; i++) {
                if (filelist[i].isDirectory()) {
                    fileDirectory[temp_d] = filelist[i];
                    num_Directory++;
                    temp_d++;
                } else {
                    file[temp_f] = filelist[i];
                    temp_f++;
                }
            }
            fileDirectory = this.indexFiles(fileDirectory, num_Directory);
            file = this.indexFiles(file, filelist.length - num_Directory);

            for (int i = 0; i < num_Directory; i++) {
                tempNode_c = new DefaultMutableTreeNode(fileDirectory[i].toString());
                tempNode_p.add(tempNode_c);
                tempNode_c.add(new DefaultMutableTreeNode(Traversal.TEMP_FILE_NAME));
            }
            for (int i = 0; i < filelist.length - num_Directory; i++) {

                tempNode_c = new DefaultMutableTreeNode(file[i].toString());
                tempNode_p.add(tempNode_c);
            }
        }
        return ;
    }

    private File[] indexFiles(File []f_p,int num_p){
        File []f = f_p;
        int num = num_p;
        for(int i=0;i<num;i++){
            for(int j=num-1;j>i;j--){
                File temp ;
                if(f[i].toString().compareToIgnoreCase(f[j].toString())>0){
                    temp = f[j];
                    f[j] = f[i];
                    f[i] = temp;
                }
            }
        }
        return f;
    }

Access to the root directory and add the time for the jTree Listener

DefaultMutableTreeNode top = new DefaultMutableTreeNode("My Computer...");
        for(int i=0;i<_root.length;i++){
            File f = new File(_root[i]);

            if(f.isDirectory()){
                DefaultMutableTreeNode tempnode = new DefaultMutableTreeNode(_root[i]);
                tempnode.add(new DefaultMutableTreeNode(Traversal.TEMP_FILE_NAME));
                top.add(tempnode);
            }
        }

        jTree1 = new JTree(top);
        jTree1.addMouseListener(new MyPanel_local_jTree1_MouseListener(this));
        jTree1.addTreeWillExpandListener(new MyPanel_local_jTree1_WillExpandLIsterner(this));
        this.jp1.updateUI();
        jscro1.getViewport().add(jTree1);

When the mouse clicks

public void jTree1_MouseListener(MouseEvent event) {
        if(event.getClickCount()==1){
            DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) jTree1.getSelectionPath().getLastPathComponent();
            new Traversal("").traversalNode(treeNode);
        }
   }

When the mouse right-click the time

public void jTree1_RightMouseListener(MouseEvent event){
       jTree1.add(jPopupMenu1);
       TreePath path = jTree1.getPathForLocation(event.getX(), event.getY());
       if(path == null)
           return;
       jTree1.setSelectionPath(path);
       DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) jTree1.getSelectionPath().getLastPathComponent();
       if(treeNode.getChildCount()>0)
           return;
       jPopupMenu1.show(event.getComponent(),event.getX(),event.getY());
   }

When will the time to start

public void jTree1_WillExpandListener(TreeExpansionEvent event){
       DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) event.getPath().getLastPathComponent();
       new Traversal("").traversalNode(treeNode);
   }

These are only part of the code, for a complete code, please call [email protected]

相关文章
  • jTree to create hard drive file directory tree 2010-06-13

    Option One: the hard drive using recursive traversal file, and add child nodes Possible degrees: the end of reasons: because we traverse the entire hard disk, the speed is very slow Option Two: Expand the node to dynamically add a child node Possible

  • New features of Java SE 7 file operations (9) - traverse the directory tree 2010-04-13

    Transferred from the developer's sky In some cases, we may need to traverse the entire directory tree, such as the need to find all. Java file. Java SE 7 provides a very convenient way to achieve this kind of functionality. Implementation of Java SE

  • [I / O stream] in a directory, create a text file to write the current file creation time, file name format requirements YYYYmmDDHHMMss.txt 2010-10-06

    import java.io.IOException; import java.io.RandomAccessFile; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * In a directory, create a text file, write to the file creation time the current , File name

  • Designed LDAP directory tree 2009-03-31

    Original: Michael Donnelly Translation: Beta Xmu / Grind Original: http://www.ldapman.org/articles/tree_design.html At first glance, the design of a LDAP directory server topology seems to be very troublesome thing. But as long as you pre-planned, an

  • linux file directory 2010-03-06

    The difference between Linux distributions actually very little difference is mainly manifested in the systems management tools and features of the different package management, in addition, no big difference; Fedora package management tools such as

  • Python3 operation file, directory and path 2010-05-14

    1. Traverse the folders and files import os import os.path rootdir = "d:/test" for parent,dirnames,filenames in os.walk(rootdir): # case 1: for dirname in dirnames: print ( "parent is:" + parent) print ( " dirnames is:" + dir

  • In the Web application to dynamically create a PDF file 2010-03-29

    In the Web application to dynamically create a PDF file In the Web application to dynamically create a PDF file text: Sean C. Sullivan translation: gagaghost In a recent logistics projects, the customer asked us to let the user build a query from a l

  • [Linux] file directory structure 2010-04-25

    Linux's file directory organized into a tree structure, level of top-level directory is the root directory, use the / said. From this directory can access all other directories. The following list briefly describes the standard Linux directory. / Roo

  • [Change] linux file directory structure Introduction 2010-05-14

    Of: Cai Xiao Jianbo off: Xiaomao imaginings Overall, the poor classification / Media to mount usb storage devices, DVD, CD-ROM, etc. / Mnt for temporary mounting file systems, removable devices should be mounted to / media up / Home user than root di

  • create a good project directory cvs server to download from the project did not solve the problem src and web directory 2010-07-20

    Create a good project directory, share to the server, but the import directory from the server, but not src and web directory. Cause: The directory does not file. The solution: create a good class in the package file or jsp file and then uploaded to

  • linux build file / directory link 2010-08-16

    1. Usage: ln [option] source_file dist_file -F build, it will be deleted with the file name. -I ask before delete. ln-s abc cde abc soft connection established ln abc cde abc's hard to establish connection 2. Soft links and hard links to the differen

  • Python in the file directory traversal of the document template from python 2010-08-28

    import os, sys from stat import * def walktree(top, callback): '''recursively descend the directory tree rooted at top, calling the callback function for each regular file''' for f in os.listdir(top): pathname = os.path.join(top, f) mode = os.stat(pa

  • Create a split development directory environment 2010-11-06

    Transfer from: http://edocs.weblogicfans.net/wls/docs92/programming/splitcreate.html # wp1102496 Create a split development directory environment The following sections describe the WebLogic Server split development directory created (which can be us

  • I used Linux command of the tree - to list the contents of the directory tree 2010-12-03

    I used Linux command of the tree - to list the contents of the directory tree This link: http://codingstandards.iteye.com/blog/833035 (reproduced please indicate the source) Use Description tree command to list the contents of the directory tree (lis

  • VMware install Linux, the error - not found in which to create a new file system and effective equipment 2011-02-20

    Problem: VMware to install RedHat Linux when: error - not found in which to create a new file system and effective equipment. Please check your hardware to find the cause of the problem. Reasons: VM is the default SCSI hard drive (when installing the

  • I used the Linux command chmod - change file / directory access permissions 2011-08-03

    I used the Linux command chmod - change file / directory access permissions This link: http://codingstandards.iteye.com/blog/1139523 (reproduced please indicate the source) Use Description chmod to change file or directory access permissions. Linux /

  • ORA-09925: Unable to create audit trail file 2011-03-16

    sqlplus / as sysdba login reported an error ORA-09925: Unable to create audit trail file One reason: directory permissions problem oracle bin directory (actually the whole oracle directory) of the owner is changed Solution chown-R oracle.oinstall / o

  • linux learning - the file directory of the cp, rm, mv 2011-09-07

    Copy, delete and move: cp, rm, mv To copy files, use the cp (copy) of this directive can, however, cp can use this command more, in addition to simply copy, you can also create a link file (that is, a shortcut is), than the two files old and be updat

  • mysql error, SQL / DB Error errcode 13 Can't create / write to file 2011-06-11

    SQL / DB Error errcode 13 Can't create / write to file error Because there is no writable temporary directory, Configuration file, tmpdir = / tmp directory has write permission can //////////////////////// How do I fix the following error SQL / DB Er

  • Oracle data file directory approach to obtain 2011-10-07

    Is as simple as a statement: select substr (file_name, 1, instr (file_name, '\', -1)) from dba_data_files where rownum = 1; Access to the data file directory, can be done using a script to create table space, or add data files. You can use PLSQL to a