我想从我的项目中的JFileChooser使选定文件的Java Jtree

时间:2019-03-12 08:00:14

标签: java swing jtree filetree

这是我的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类,我在其中清除树的功能

这是调用文件选择器函数的类

    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");
        }
    }

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

0 个答案:

没有答案