Java-implemented Windows Explorer instance _java

Source: Internet
Author: User

This example describes the Windows Explorer for the Java implementation. Share to everyone for your reference. Specifically as follows:

Filetree.java files are as follows:

Filetree.java/*********************************************************** * Author:jason * EMAIL:TL21CEN@HOTMA
il.com * CSDN blog:http://blog.csdn.net/unagain/***********************************************************/
Package tl.exercise.swing;
Import java.awt.Component;
Import Java.io.File;
Import Java.util.Vector;
Import Javax.swing.Icon;
Import Javax.swing.JTree;
Import javax.swing.event.TreeExpansionEvent;
Import Javax.swing.event.TreeExpansionListener;
Import Javax.swing.event.TreeModelListener;
Import javax.swing.event.TreeSelectionEvent;
Import Javax.swing.event.TreeSelectionListener;
Import Javax.swing.filechooser.FileSystemView;
Import Javax.swing.tree.DefaultTreeCellRenderer;
Import Javax.swing.tree.TreeModel;
Import Javax.swing.tree.TreePath;
  public class Filetree extends JTree {static final long serialversionuid = 0;
  private FileList thelist;
    Public Filetree (filelist list) {thelist = list;
   Setmodel (New Filesystemmodel (New Foldernode ())); This.setcellrenderer (New Folderrenderer ());
    Addtreeselectionlistener (New Treeselectionlistener () {public void valuechanged (Treeselectionevent tse) {}   
    });
  This.setselectionrow (0);
    } public void firevaluechanged (Treeselectionevent tse) {treepath TP = Tse.getnewleadselectionpath ();
    Object o = tp.getlastpathcomponent ();
    Thelist.firetreeselectionchanged ((Pathnode) o);
  Thelist.firetreeselectionchanged ((Foldernode) o);
    public void firetreecollapsed (TreePath path) {super.firetreecollapsed (path);
    TreePath Curpath = Getselectionpath ();
    if (Path.isdescendant (Curpath)) {Setselectionpath (path);
  } public void Firetreewillexpand (TreePath path) {System.out.println ("path would expand is" + path);
  public void Firetreewillcollapse (TreePath path) {System.out.println ("path would collapse is" + path);
    Class Expansionlistener implements Treeexpansionlistener {Filetree tree; Public expansionListener (filetree ft) {tree = ft;
    public void treecollapsed (Treeexpansionevent tee) {} public void treeexpanded (Treeexpansionevent tee) {
  }} class Filesystemmodel implements TreeModel {I_filesystem theroot;
  char fileType = i_filesystem.directory;
  Public Filesystemmodel (I_filesystem fs) {theroot = FS;
  Public Object Getroot () {return theroot;
  public Object Getchild (object parent, int index) {return (I_filesystem) parent). Getchild (FileType, index);
  public int Getchildcount (Object parent) {return (I_filesystem) parent). Getchildcount (FileType);
  public boolean IsLeaf (Object node) {return (I_filesystem) node). IsLeaf (FileType);  public int Getindexofchild (object parent, Object child) {return (I_filesystem) parent). Getindexofchild (FileType,
  Child); public void valueforpathchanged (TreePath path, Object newvalue) {} public void Addtreemodellistener (treemodellist Ener l) {} Public void Removetreemodellistener (Treemodellistener l) {}} interface I_filesystem {final public static char DIRECTORY
  = ' D ';
  Final public static char FILE = ' F ';
  Final public static char all = ' A ';
  Public Icon GetIcon ();
  Public I_filesystem Getchild (char fileType, int index);
  public int Getchildcount (char fileType);
  public boolean isleaf (char fileType);
public int Getindexofchild (char FileType, Object child); }/** * A data model for a jtree.
 This Model Explorer Windows file system directly. * * <p> * Perhaps there is a fatal bug and this design. For speed, each of instances * of the This model contains file objects of subdirectory, up to now, there isn ' t To release them until the program is end. 
 I ' m afraid that the memory * would being full of if the file system is large enough and JVM memery size * setted too small. * * <p> * I won ' t pay more attention to solve it.
 It isn ' t goal of current a exercise. * @author Jason * * Class FoldernodE implements I_filesystem {//private static Foldernode theroot;
  private static FileSystemView FSView;
  private static Boolean Showhiden = true;;
  Private File thefile;
  Private vector<file> all = new vector<file> ();
  Private vector<file> folder = new vector<file> ();
   /** * Set that whether apply Hiden file.
  * * @param ifshow */public void Setshowhiden (Boolean ifshow) {Showhiden = Ifshow;
  Public Icon GetIcon () {return Fsview.getsystemicon (thefile);
    Public String toString () {//return FSView.
  Return Fsview.getsystemdisplayname (thefile); }/** * Create a root node.
   By default, it should is the DeskTop in window file * System.
    * */Public Foldernode () {FSView = Filesystemview.getfilesystemview ();
    Thefile = Fsview.gethomedirectory ();
  Preparechildren ();
    private void Preparechildren () {file[] files = Fsview.getfiles (Thefile, Showhiden); for (int i = 0; i < Files.lenGth
      i++) {All.add (files[i]); if (Files[i].isdirectory () &&!files[i].tostring (). toLowerCase (). EndsWith (". lnk")) {Folder.add (
      Files[i]);
    }} private Foldernode (file file) {thefile = file;
  Preparechildren (); Public Foldernode Getchild (char fileType, int index) {if (i_filesystem.directory = = FileType) {return new
    Foldernode (Folder.get (index));
    else if (I_filesystem.all = = FileType) {return new Foldernode (All.get (index));
    else if (i_filesystem.file = = fileType) {return null;
    else {return null; } public int Getchildcount (char fileType) {if (i_filesystem.directory = = FileType) {return folder.size ()
    ;
    else if (I_filesystem.all = = FileType) {return all.size ();
    else if (i_filesystem.file = = FileType) {return-1;
    else {return-1; } public boolean isleaf (char fileType) {if i_filesystem.directory = = FilEType) {return folder.size () = 0;
    else if (I_filesystem.all = = FileType) {return all.size () = = 0;
    else if (i_filesystem.file = = FileType) {return true;
    else {return true; } public int Getindexofchild (char FileType, Object child) {if (child instanceof Foldernode) {if (i_files Ystem.
      DIRECTORY = = FileType) {return Folder.indexof ((foldernode) child). Thefile);
      else if (I_filesystem.all = = FileType) {return All.indexof ((foldernode) child). Thefile);
      else if (i_filesystem.file = = FileType) {return-1;
      else {return-1;
    } else {return-1;
  }} class Folderrenderer extends Defaulttreecellrenderer {private static final long serialversionuid = 1L;  Public Component gettreecellrenderercomponent (jtree tree, Object value, Boolean sel, Boolean expanded, Boolean leaf, int row, Boolean hasfocus) {i_filesystem node = (i_filesystem) Value
    Icon icon = Node.geticon ();
    Setleaficon (icon);
    Setopenicon (icon);
    Setclosedicon (icon);
  Return Super.gettreecellrenderercomponent (tree, value, sel, expanded, leaf, row, hasfocus); }
}

I hope this article will help you with your Java programming.

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.