繁体   English   中英

如何更改JTree中空文件夹的图标? (FTP文件)

[英]How to change icon of a empty folder in JTree? (FTP File)

我正在努力将FTPserver的所有文件和文件夹显示到JTree中。 但是我有一个问题,空文件夹显示为文件。 但是如何将它们显示为文件夹图标?

这是我的代码:

public void buildTree(){

    try {
        ftpClient.connect("130.229.178.31");             
        ftpClient.login("admin", "123456");

        root = new DefaultMutableTreeNode("Welcome!");        
        for (int i = 0; i < 1; i++) {
            DefaultMutableTreeNode temp = new DefaultMutableTreeNode("FTP-Server");
            root.add(temp);
            bind(temp,"");
        }   
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException("Client Error", e1);
    }
    try {
            ftpClient.disconnect();
    } catch (IOException e2) {
            e2.printStackTrace();
            throw new RuntimeException("Error when shutdown", e2);
    }
}   

// bind nod/subnode to the tree (recursive method)
public void bind(DefaultMutableTreeNode node,String path){
    try {

        Boolean defaultPath = true;
        while (defaultPath)
        {
            defaultPath = ftpClient.changeToParentDirectory();
        }

        ftpClient.changeWorkingDirectory(path);

        FTPFile[] files = ftpClient.listFiles();


        for(int i=0;files!=null && i<files.length;i++){
            FTPFile tempFile = files[i];
            if(tempFile.isDirectory()){




                DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(tempFile.getName());
                node.add(tempNode);


                bind(tempNode, path+"/"+tempFile.getName());
            }else{
                DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(tempFile.getName());
                node.add(tempNode);
            }
        }


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
} 

FTP J树

“ sad”文件夹是一个空文件夹,但显示为文件图标。 怎么改变呢?

非常感谢你

PS:相同的方法无法正常工作,例如

FileSystemView fileSystemView = FileSystemView.getFileSystemView();
setIcont(fileSystemView.getSystemIcon(File file));

因为我们使用的是FTP文件,而不是文件。

您有两种选择:

  • 将“虚拟文件”添加到一个空文件夹(可能称为“ [空]”)或
  • 使用您自己的DefaultTreeCellRenderer

我认为第二个建议是可取的。 我还建议使用您自己的TreeModel来指示树节点是文件还是文件夹。

暂无
暂无

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

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