2017-07-24 50 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

非常感谢你的这个想法。我想不起来。但如果你知道这个方法,请让我知道:) –