簡體   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