实现指定文件目录下的文件树,然后对结点的增删可以同时作用于文件

xiaoxiao2021-02-28  100

用自定义的文件树来定义结点后,Jtree默认的对结点的增删函数将不再起作用,也不能将对结点的操作作用于对应的文件;

对文件同时进行操作的思想是,先将结点与文件的联系断开,对结点进行初始化,即可利用系统自带的增删改函数,对结点操作后,在与相应的文件联系起来

 public void setPopupMenu(FileTree tree) {           final JPopupMenu pop = new JPopupMenu();           //final  FileSystemView fileSystemView = FileSystemView.getFileSystemView();         pop.add(new AbstractAction("添加子目录") {               private static final long serialVersionUID = 1L;               @Override             public void actionPerformed(ActionEvent e) {              TreePath treepath = tree.getSelectionPath();              DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) treepath.getLastPathComponent();              FileNode fileNode=(FileNode)selectionNode.getUserObject();                TreeNode parentNode = selectionNode.getParent();            try{                   String s = fileNode.file.getAbsolutePath();                            String fileName = new String(s.getBytes("GB2312"),"GBK");                                CreateFileUtil newDir = new CreateFileUtil(fileName);                  newDir.createDir();                                     //定义新结点            DefaultMutableTreeNode newNode=new DefaultMutableTreeNode();            newNode.setAllowsChildren(true);                         File tempFile = new File(newDir.destFileName);             System.out.println(newDir.destFileName);           // System.out.println(newDir.dir.getName());                                  FileNode tempNode = new FileNode(newDir.dir.getName(),null,tempFile,false);                           newNode=new DefaultMutableTreeNode(tempNode);            ((DefaultTreeModel) tree.getModel()).insertNodeInto(newNode,selectionNode,selectionNode.getChildCount());                       }            catch(UnsupportedEncodingException eve){            }            }                       });           pop.add(new AbstractAction("添加文件") {               private static final long serialVersionUID = 1L;              @Override             public void actionPerformed(ActionEvent e) {                   TreePath treepath = tree.getSelectionPath();                   DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) treepath.getLastPathComponent();                   FileNode fileNode=(FileNode)selectionNode.getUserObject();                     DefaultMutableTreeNode parentNode =(DefaultMutableTreeNode) selectionNode.getParent();                   FileNode parentfileNode=(FileNode)parentNode.getUserObject();          if(fileNode.file.isDirectory()){              RenameDialog re = new RenameDialog(null,true);              re.setLocationRelativeTo(null);              re.setVisible(true);                            String inputname = re.getname();                                       try{                   String realName = fileNode.file.getAbsolutePath()+"\\"+inputname;                          String fileName = new String(realName.getBytes("GB2312"),"GBK");                                CreateFileUtil newFile = new CreateFileUtil(fileName);                  newFile.createFile();                   //定义新结点            DefaultMutableTreeNode newNode=new DefaultMutableTreeNode();            newNode.setAllowsChildren(true);                        FileSystemView fileSystemView = FileSystemView.getFileSystemView();                         File tempFile = new File(newFile.destFileName+".txt");             FileNode tempNode = new FileNode(newFile.file.getName().replace(".txt",""),null,tempFile,false);                           newNode=new DefaultMutableTreeNode(tempNode);            ((DefaultTreeModel) tree.getModel()).insertNodeInto(newNode,selectionNode,selectionNode.getChildCount());                       }            catch(UnsupportedEncodingException eve){            }           tree.updateUI();          }       }       });             pop.add(new AbstractAction("删除目录") {               private static final long serialVersionUID = 1L;                  @Override             public void actionPerformed(ActionEvent e) {               TreePath treepath = tree.getSelectionPath();                 DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) treepath.getLastPathComponent();                 FileNode fileNode=(FileNode)selectionNode.getUserObject();                  TreeNode parentNode = selectionNode.getParent();                                  try{               if(!(fileNode.file.getAbsolutePath()).equals("C:\\Users\\Dadaism\\Desktop\\123\\用户文件夹")){              String s = fileNode.file.getAbsolutePath();                String fileName = new String(s.getBytes("GB2312"),"GBK");              if(fileNode.file.isFile()){              new DeleteUtil().deleteFile(fileName);              }              if(fileNode.file.isDirectory()){              new DeleteUtil().deleteDirectory(fileName);              }                              FileNode tempNode = new FileNode(null,null,null,false);                           ((DefaultTreeModel) tree.getModel()).removeNodeFromParent(selectionNode);                 }              else{              System.out.println("不能删除根目录!");              }                  }catch(UnsupportedEncodingException eve){               }              }         });                     tree.addMouseListener(new MouseAdapter() {               @Override               public void mouseReleased(MouseEvent e) {                   if (e.isMetaDown()) {                       pop.show(tree, e.getX(), e.getY());                   }               }             });           }

相关的类

import java.io.File; import java.io.IOException;   public class CreateFileUtil {     private int i = 1;     public String destFileName;     File dir;     File file;     CreateFileUtil(String fileName){         destFileName = fileName;     }     public void createFile() {     String name = destFileName;         String addtxt = name+".txt";         file = new File(addtxt);                  while(file.exists()){         destFileName = name+"("+(i++)+")";                 addtxt = destFileName + ".txt";         file = new File(addtxt);         }         try {             file =new File(destFileName+".txt");            file.createNewFile();                     } catch (IOException e) {         }     }        public void createDir() {     String name = destFileName+"\\新建文件夹";         destFileName =name;         dir= new File(name);         while(dir.exists()){         destFileName = name+"("+(i++)+")";         dir =new File(destFileName);         }         //创建目录         dir.mkdirs();                  } } 需要注意的是,java默认编码方式为UTF-8,获得路径时可能会导乱码现象,可以用getBytes()函数进行转化

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

最新回复(0)