简体   繁体   English

为 FileExplorerApplication 创建树,但加载速度太慢:<(JAVA、NETBEAN、JTREE、文件资源管理器)

[英]Make an Tree for FileExplorerApplication but it's too slow for loading :< (JAVA , NETBEAN, JTREE, FILE Explorer)

So I want to make Tree for File Explorer, But my code is very slow:< I want to increase Perform of this application (sr My English is not very good):所以我想为文件资源管理器制作树,但是我的代码很慢:<我想增加这个应用程序的执行(sr我的英语不是很好):

This is What i Want but it's load too slow 1-2min这就是我想要的,但它的加载速度太慢了 1-2 分钟

This is design view这是设计视图

So this is my code: (I remove initcomponent b/c it's not needed)所以这是我的代码:(我删除了不需要的 initcomponent b/c)

public class TreePanel extends javax.swing.JPanel {

    DefaultMutableTreeNode root;
    File[] drives;
    File copyfile,movefile;
    public TreePanel() {
        initComponents();
        drives = GetDrive();
        CreateJtree(drives);
        //ChangeIconNode();

    }
private javax.swing.JTree MyTree;
private javax.swing.JScrollPane jScrollPane1;

private void CreateJtree(File[] drives1) {
    root = new DefaultMutableTreeNode("This PC");

    DefaultTreeModel tree = new DefaultTreeModel(root);
    MyTree.setModel(tree);
    if (drives1 != null) {
        for (File file : drives1) {
            DefaultMutableTreeNode tmp = new DefaultMutableTreeNode(file.getAbsolutePath());
            root.add(tmp);
            AddNodeByRecursive(tmp, file.listFiles());
        }

    }
}

//Image //图片

private Icon ImageResize(ImageIcon imageIcon) {
    int size = 15;
    Image t = imageIcon.getImage().getScaledInstance(size, size, Image.SCALE_SMOOTH);
    return new ImageIcon(t);
}

private void ChangeIconNode() {
    // Change the default JTree icons
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) MyTree.getCellRenderer();

    Icon closedIcon = ImageResize(new ImageIcon("src/ImagePackages/closed.png"));
    Icon openIcon = ImageResize(new ImageIcon("src/ImagePackages/open.png"));
    Icon leafIcon = ImageResize(new ImageIcon("src/ImagePackages/leaf.png"));
    renderer.setClosedIcon(closedIcon);
    renderer.setOpenIcon(openIcon);
    renderer.setLeafIcon(leafIcon);
    //end of change
}

private File[] GetDrive() {
    return File.listRoots();
}

private void AddNodeByRecursive(DefaultMutableTreeNode root, File[] drives1) {
    if (drives1 == null) {
        return;
    } else {
        for (File file : drives1) {
            DefaultMutableTreeNode tempnode = new DefaultMutableTreeNode(file.getName());
            System.out.println(file.getName());
            root.add(tempnode);
            File[] tempfilearr = file.listFiles();
            AddNodeByRecursive(tempnode, tempfilearr);
        }

    }
}

private void RefreshTree() {
    
    DefaultTreeModel tree = new DefaultTreeModel(root);
    MyTree.setModel(tree);
}

but it's load too slow 1-2min但它的加载速度太慢了 1-2 分钟

Sounds like you are trying to build the tree for the entire drive up front.听起来您正在尝试为整个驱动器预先构建树。 Don't.不。

Instead, you can dynamically build the tree as the user clicks to expand a node by adding a TreeWillExpand listener to the tree.相反,您可以通过向树添加TreeWillExpand侦听器,在用户单击展开节点时动态构建树。

Simple example:简单的例子:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.nio.file.*;

public class TreeFileSystem extends JPanel
    implements ActionListener, TreeWillExpandListener
{
    private int fileIndex;

    private JTree tree;
    private DefaultTreeModel model;

    private JTextField textField;

    public TreeFileSystem()
    {
        super(new BorderLayout());

        textField = new JTextField("c:\\");
        textField.addActionListener( this );
        add(textField, BorderLayout.NORTH);

        tree = new JTree( new DefaultMutableTreeNode() );
        JScrollPane scrollPane = new JScrollPane( tree );
        add(scrollPane);

        tree.addTreeWillExpandListener( this );

        textField.postActionEvent();
    }

    public void actionPerformed(ActionEvent e)
    {
        File file = new File( textField.getText() );

        if (file.exists())
        {
            DefaultMutableTreeNode root = new DefaultMutableTreeNode( file );
            model = new DefaultTreeModel( root );
            addNodes(root, true);
            tree.setModel( model );
        }
    }

    private void addNodes(DefaultMutableTreeNode root, boolean addChildNodes)
    {
        File file = (File)root.getUserObject();
        System.out.println(file);

        if (!file.isDirectory()) return;

        File[] files = file.listFiles(f -> !f.isHidden());

        if (files == null) return;

        int directoryInsert = 0;

        for (int i = 0; i < files.length; i++)
        {
            file = files[i];

            DefaultMutableTreeNode node = new MyDefaultMutableTreeNode( file );

            if (file.isDirectory())
                root.insert(node, directoryInsert++);
            else
                root.insert(node, i);

            if (addChildNodes)
                addNodes(node, false);
        }
    }

    public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException
    {
        System.out.println("will expand");
        if (tree.hasBeenExpanded(e.getPath())) return;

        TreePath path = e.getPath();
        System.out.println(path);

        if (path.getPathCount() == 2) return;
        System.out.println(path.getPathCount());

        DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getPathComponent( path.getPathCount() - 1);
        addNodes(node, false);
    }

    public void treeWillCollapse(TreeExpansionEvent e) {}

    class MyDefaultMutableTreeNode extends DefaultMutableTreeNode
    {
        public MyDefaultMutableTreeNode(Object node)
        {
            super(node);
        }

        public boolean isLeaf()
        {
            File file = (File)getUserObject();

            return file.isFile();
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().add( new TreeFileSystem() );
        frame.setSize(300, 400);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM