2017-07-24 48 views
0

我是新來JTrees並有一個查詢:添加到JTree的JFileChooser的

如果我們能在JFileChooser添加文件和可用目錄的tree在我們的系統。基本上我想有一個自定義JFileChooser,其中的文件和目錄可以以樹的形式顯示。

感謝提前:)

我創建了正在我的系統目前的文件,但如何顯示在樹JFileChooser的一棵樹。這裏是JTree的代碼

public class FileTree extends JPanel { 

/** Construct a FileTree */ 
    public FileTree(File dir) { 
    setLayout(new BorderLayout()); 

    // Make a tree list with all the nodes, and make it a JTree 
    JTree tree = new JTree(addNodes(null, dir)); 

    // Lastly, put the JTree into a JScrollPane. 
    JScrollPane scrollpane = new JScrollPane(); 
    scrollpane.getViewport().add(tree); 
    add(BorderLayout.CENTER, scrollpane); 
    } 

    /** Add nodes from under "dir" into curTop. Highly recursive. */ 
    DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) 
{ 
    String curPath = dir.getPath(); 
    DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath); 
    if (curTop != null) { // should only be null at root 
    curTop.add(curDir); 
    } 
    Vector ol = new Vector(); 
    String[] tmp = dir.list(); 
    for (int i = 0; i < tmp.length; i++) 
    ol.addElement(tmp[i]); 
    Collections.sort(ol, String.CASE_INSENSITIVE_ORDER); 
    File f; 
    Vector files = new Vector(); 
    // Make two passes, one for Dirs and one for Files. This is #1. 
    for (int i = 0; i < ol.size(); i++) { 
    String thisObject = (String) ol.elementAt(i); 
    String newPath; 
    if (curPath.equals(".")) 
     newPath = thisObject; 
    else 
    newPath = curPath + File.separator + thisObject; 
    if ((f = new File(newPath)).isDirectory()) 
    addNodes(curDir, f); 
    else 
    files.addElement(thisObject); 
    } 
    // Pass two: for files. 
    for (int fnum = 0; fnum < files.size(); fnum++) 
    curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum))); 
    return curDir; 
    } 

    public Dimension getMinimumSize() { 
    return new Dimension(200, 400); 
    } 

    public Dimension getPreferredSize() { 
    return new Dimension(200, 400); 
    } 

    /** Main: make a Frame, add a FileTree */ 
    public static void main(String[] av) { 

    JFrame frame = new JFrame("FileTree"); 
    frame.setForeground(Color.black); 
    frame.setBackground(Color.lightGray); 
    Container cp = frame.getContentPane(); 

    if (av.length == 0) { 
    cp.add(new FileTree(new File("."))); 
    } else { 
    cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS)); 
    for (int i = 0; i < av.length; i++) 
    cp.add(new FileTree(new File(av[i]))); 
    } 

    frame.pack(); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
    } 
+1

如果您的問題僅是答案是肯定的。如果你想知道如何,那麼你的問題太廣泛,無法回答。改爲詢問具體問題。 – talex

+0

我知道這是廣泛的,這就是爲什麼我無法理解從哪裏開始,如果我需要做這樣一個項目。 @talex。謝謝:) –

+0

@bhavna garg你可以在'FileChooser'上使用'setAccesory(FileTree)'這種方法在'FileChooser'的右邊添加'FileTree'。 –

回答

0

這是一個非常寬泛的問題,但這裏有幾個指針。

如果您嘗試將它與jFileChooser集成,事情可能會變得混亂,但有可能。

我建議你改用你的代碼,但要做一些改動,就像jFileChooser一樣。

首先:向您的FileTree面板添加一個選擇/打開按鈕。當按下此按鈕時,您可以快速獲取所選文件的列表並將其保存到共享變量中。其次:你可以使用一個共享變量將信息從FileTree彈出窗口傳遞迴它被調用的線程(you could also have a change listener checking for the updated variable),然後你可以用返回的文件完成你想要的任務,就像返回來自jFileChooser的聲明。

注: 確保你只叫你的FileTreefrom a separate thread than the EDT(或者你可以阻止EDT,但是這通常不是一個好主意):「這是可能的」

+0

非常感謝你的這個想法。我想不起來。但如果你知道這個方法,請讓我知道:) –