首页 > 解决方案 > 我想在我的项目中从 JFileChooser 中制作所选文件的 java Jtree

问题描述

这是我的 TreeModel 类

    BusinessLayer;

    import java.io.File;
    import javax.swing.event.TreeModelListener;
    import javax.swing.tree.TreeModel;
    import javax.swing.tree.TreePath;

    /**
     *
     * @author attam
     */
    public class FileTreeModel implements TreeModel
    {
        // Attributes
        private File root;

        /**
         *  Constructor initializes the root of TreeModel
         * @param root directory
         */
        public FileTreeModel(File root)
        {
            this.root = root;
        }

        /**
         *  Returns root of the TreeModel i.e. JTree
         * @return root
         */
        public Object getRoot()
        {
            return root;
        }

        /**
         * Check if given node is root or not
         * @param node the node to be checked
         * @return true if it is root else false
         */
        public boolean isLeaf(Object node)
        {
            return ((File)node).isFile();
        }

        /**
         * Returns number of child nodes in JTree
         * @param parent Parent node
         * @return no of child nodes
         */
        public int getChildCount(Object parent)
        {
            String[] children = ((File)parent).list();
            if (children == null) return 0;
            return children.length;
        }

        /**
         * Returns the child node by its index
         * @param parent Parent Node
         * @param index  Index of child node
         * @return child node
         */
        public Object getChild(Object parent, int index)
        {
            String[] children = ((File)parent).list();
            if ((children == null) || (index >= children.length))
                return null;
            return new File((File) parent, children[index]);
        }

        /**
         * Return index of child node
         * @param parent Parent of child
         * @param child  child node
         * @return index of child node
         */
        public int getIndexOfChild(Object parent, Object child)
        {
            String[] children = ((File)parent).list();
            if (children == null)
                return -1;
            String childname = ((File)child).getName();
            for(int i = 0; i < children.length; i++)
            {
                if (childname.equals(children[i]))
                    return i;
            }
                return -1;

        }
        @Override
        public void valueForPathChanged(TreePath path, Object newvalue) {}

        @Override
        public void addTreeModelListener(TreeModelListener l) {}

        @Override
        public void removeTreeModelListener(TreeModelListener l) {}

    }

这是我清除树功能的 TreeModel 类

这是调用 filechooser 函数的类

    package BusinessLayer;

    import java.io.File;
    import javax.swing.JFileChooser;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    /**
     * @author Mohsin Atta
     * @Reg # 3258-BSSE/F16
     */

    public class FileInformation {
        public static JFileChooser fileChooser = new JFileChooser();
        public static List<File> allFiles;
        public static String folderName;




        public static File getAllFiles() {
             List<File> fileList=new ArrayList<>();

            // JButton open = new JButton();
            if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
            {
                File files[] = fileChooser.getCurrentDirectory().listFiles();
                fileList= Arrays.asList(files);
                allFiles=fileList;
                return fileChooser.getCurrentDirectory();
            }
            return null;

        }

        public static List<File> getJavaFiles() {
            List<File> fileList=new ArrayList<>();
            for(File file:allFiles)
            {
                if(file.getName().endsWith(".java"))
                {
                    fileList.add(file);
                    ClassAndMethodExtractor obj = new `ClassAndMethodExtractor(file);`
                    obj.getPublicClass();
                    obj.getPublicMethods();
                }

            }
            return fileList;
        }
    }

在 getAllFiles 函数中的上述文件中

这是我设置树但没有显示树的功能

    public static void setFileList(File file) throws IOException 
    {
        String setFolder;
       // setFolder = folder+" Accessed";
        if (file != null) 
        {
            TreeModel model = new FileTreeModel(file);
            tree.setModel(model);
                //allFilesArea.append(file.getCanonicalPath());
                //allFilesArea.append("\n");
        }
    }

我面临的问题是我声明了树并根据方法使用它,但它仍然没有显示树,也没有给出任何错误..所以请给出解决方案或任何其他替代方法来制作树文件路径

标签: javaswingjtreefiletree

解决方案


推荐阅读