Results 1 to 2 of 2
  1. #1
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default Problems with JTree

    Hi again!

    I would like once again to ask your professional feedback. So, I have JTree that displays the content of "D:\\docmngsyst\\docs". I cannot find the answer for the following question:
    *) How to make my tree opened. I mean that initially I see only root node without any other nodes. But I need to display whole tree (at least two levels).

    Thanks for any idea or a link to tutorial!

    PHP Code:
            DefaultMutableTreeNode master = new DefaultMutableTreeNode("Documents");
            tree = new JTree(master);
            listAllFiles("D:\\docmngsyst\\docs",  master, true);
    PHP Code:
        public static void listAllFiles(String directory, DefaultMutableTreeNode parent, Boolean recursive) {
             File [] children = new File(directory).listFiles();
             for (int i = 0; i < children.length; i++) {
                       DefaultMutableTreeNode node = new DefaultMutableTreeNode(children[i].getName());
                       if (children[i].isDirectory() && recursive) {
                             parent.add(node);
                             listAllFiles(children[i].getPath(), node, recursive);
                       } else if (!children[i].isDirectory()){
                       parent.add(node);
                  }
             }
        }
    Last edited by LianaN; 09-18-2010 at 02:31 PM.

  2. #2
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Uf, it was a bit stupid question. I just had to change two lines of code to solve the problem:

    PHP Code:
            DefaultMutableTreeNode master = new DefaultMutableTreeNode("Documents");
            listAllFiles("D:\\docmngsyst\\docs",  master, true);
            tree = new JTree(master);
    Sorry.

Similar Threads

  1. JTree/TreeModelListener
    By sQu3aKy in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2010, 08:47 AM
  2. Jtree Help
    By miladirooni in forum New To Java
    Replies: 1
    Last Post: 10-28-2009, 01:00 AM
  3. zip to JTree
    By icsbcn in forum AWT / Swing
    Replies: 8
    Last Post: 09-01-2009, 05:26 PM
  4. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 12:59 PM
  5. JTree trouble
    By Alantie Vala in forum AWT / Swing
    Replies: 3
    Last Post: 08-01-2007, 12:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •