Jtree生成系统文件树

xiaoxiao2021-02-28  87

Jtree原始的默认结点只具有虚拟意义,需要自定义文件结点代替虚拟结点,然后进行生成

class FileNode{     public FileNode(String name,Icon icon,File file,boolean isDummyRoot){         this.name=name;this.icon=icon;this.file=file;         this.isDummyRoot=isDummyRoot;     }     public boolean isInit;     public boolean isDummyRoot;     public String name;     public Icon icon;     public File file; }

每次添加文件结点是,先用自定义的文件结点初始化虚拟结点,进行树状结构的添加,从而实现文件与结点 的相关联

FileNode childFileNode = new FileNode(files[i].getName(),getIcon(files[i]),files[i], false);                         DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode(childFileNode);                         lastTreeNode.add(childTreeNode);

下面将生成系统文件树

class FileTree extends JTree {     public TreePath mouseInPath;     protected FileSystemView fileSystemView = FileSystemView.getFileSystemView();     public FileTree(){         setRootVisible(false);         addTreeWillExpandListener(new TreeWillExpandListener() {             @Override             public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {                 DefaultMutableTreeNode lastTreeNode =(DefaultMutableTreeNode) event.getPath().getLastPathComponent();                 FileNode fileNode = (FileNode) lastTreeNode.getUserObject();                 if (!fileNode.isInit) {                     File[] files;                     if (fileNode.isDummyRoot) {                         files = fileSystemView.getRoots();                     } else {                         files = fileSystemView.getFiles(                                 ((FileNode) lastTreeNode.getUserObject()).file,                                 false);                     }                     for (int i = 0; i < files.length; i++) {                         FileNode childFileNode = new FileNode(                                 fileSystemView.getSystemDisplayName(files[i]),                                 fileSystemView.getSystemIcon(files[i]), files[i],                                 false);                         DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode(childFileNode);                         lastTreeNode.add(childTreeNode);                     }                                         DefaultTreeModel treeModel1 = (DefaultTreeModel) getModel();                     treeModel1.nodeStructureChanged(lastTreeNode);                 }                                  fileNode.isInit = true;             }             @Override             public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {             }         });         addMouseMotionListener(new MouseAdapter() {             @Override             public void mouseMoved(MouseEvent e) {                 TreePath path=getPathForLocation(e.getX(), e.getY());                 if(path!=null){                     if(mouseInPath!=null){                         Rectangle oldRect=getPathBounds(mouseInPath);                         mouseInPath=path;                         repaint(getPathBounds(path).union(oldRect));                     }else{                         mouseInPath=path;                         Rectangle bounds=getPathBounds(mouseInPath);                         repaint(bounds);                     }                 }else if(mouseInPath!=null){                     Rectangle oldRect=getPathBounds(mouseInPath);                     mouseInPath=null;                     repaint(oldRect);                 }             }         });     } }

转载请注明原文地址: https://www.6miu.com/read-61817.html

最新回复(0)