Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: make a jtree from a directory

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question make a jtree from a directory

    hi.i have some problems with making a jtree.i wanna make a jtree from a directory consist of some text file.i want to have the contain of files on the tree to.and i test all of parent and child nodes and all of them have right value but i dont know why i have an IllegalArgumentException.i dont know what to do now!!!can anyone help me?:-/


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: make a jtree from a directory

    Hello ostad,

    I've made a program a few weeks back and I was waiting for an appropriate time to use it. If you would not mind testing it, it is a program designed to give the first few tips on posting here with some semi-dynamic results. It's pretty much a large amount of text that formats an output in the end, not complicated at all. Available here

    Besides that, it is necessary for you to post the code that results in an error for us to help you, and the entire error message. An error message looks like this:
    Main.java:12: cannot find symbol
    symbol  : method test()
    location: class A
             a.test();
              ^
    1 error

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re: make a jtree from a directory

    ok.this is my code and its error!
    //*****************
    public void listAllFiles(String directory, DefaultMutableTreeNode parent, Boolean recursive) throws IOException {
    try {
    File[] children = new File(directory).listFiles();
    // list all the files in the directory
    for (int i = 0; i < children.length; i++) { // loop through each
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(children[i].getName());
    // only display the node if it isn't a folder, and if this is a recursive call
    if (children[i].isDirectory() && recursive) {
    parent.add(node);
    listAllFiles(children[i].getPath(), node, recursive);
    // call again for the subdirectory
    } else if (!children[i].isDirectory()) { // otherwise, if it isn't a directory
    parent.add(node);
    }
    root = new DefaultMutableTreeNode(children[i].getName());
    FileReader infile = new FileReader(children[i].getPath());
    BufferedReader reader = new BufferedReader(infile);
    String s = reader.readLine();
    treenode = new DefaultMutableTreeNode(s);
    while (s != null) {
    if (treenode.getParent() == null) {
    treenode = new DefaultMutableTreeNode(s);
    treenode.setParent(root);
    par = (DefaultMutableTreeNode) treenode.getParent();
    par.add(treenode);
    } else {
    treenode = new DefaultMutableTreeNode(s);
    par = (DefaultMutableTreeNode) treenode.getParent();
    par.add(treenode);
    }
    s = reader.readLine();
    }
    reader.close();
    infile.close();
    }
    } catch (IOException e) {
    JOptionPane.showMessageDialog(rootPane, "Error in reading file");
    }
    }
    //**********
    IllegalArgumentException:Argument is not a child!

Similar Threads

  1. How to make a JTree have multiple layers.
    By javapenguin in forum AWT / Java Swing
    Replies: 1
    Last Post: May 31st, 2012, 07:41 AM
  2. How to copy files from one directory to another directory
    By kewlkeny in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: January 25th, 2012, 07:36 AM
  3. How to create directory in Java?
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 24th, 2011, 12:53 PM
  4. [SOLVED] Jtree help
    By sman36 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 09:39 AM
  5. How to create directory in Java?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:43 AM