2

So I have a class that gets all the files and folders and returns them as a string array. This is the output.

/applications/HI
/applications/HI/Hey.txt
/applications/HI/Milk
/applications/HI/Milk/blah
/applications/HI/Milk/HEADIAJQ/text.txt
/applications/HI/Milk/HEADIAJQ/thisworks!!!!.txt
/applications/HI/MilkiM/TRASHINGTHINGS.txt
/applications/HI/emoji.txt
/applications/Test
/applications/Test/Tjinsg

So from here, im sort of stuck. I don't have a clue how to convert this into a TreeView from javafx. Any help would be appreciated. Thanks in advance.

2
  • I would think it would be easier for the method that retrieves the files to return some kind of hierarchical structure in the first place, then to have it return a String[] and then convert that back to a hierarchical structure. This is an X-Y problem Commented Sep 29, 2016 at 16:30
  • You deleted your question before I could comment. No, Java does not have dynamic variable names, nor would you want to use them. Variable names are not that important in Java and almost don't exist in compiled code. What is much more important are references -- how you get a handle on objects. Commented Sep 29, 2016 at 21:57

2 Answers 2

2

try this.

static void add(TreeItem<String> node, String path) {
    String[] items = path.substring(1).split("/");
    for (int i = 0; i < items.length; ++i) {
        TreeItem<String> found = null;
        for (TreeItem<String> child : node.getChildren())
            if (child.getValue().equals(items[i])) {
                found = child;
                break;
            }
        if (found == null) {
            found = new TreeItem<>(items[i]);
            node.getChildren().add(found);
        }
        node = found;
    }
}

static void print(TreeItem<String> node, String indent) {
    System.out.printf("%sTreeItem: %s%n", indent, node.getValue());
    for (TreeItem<String> child : node.getChildren())
        print(child, indent + "  ");
}

and

    String[] paths = {
        "/applications/HI",
        "/applications/HI/Hey.txt",
        "/applications/HI/Milk",
        "/applications/HI/Milk/blah",
        "/applications/HI/Milk/HEADIAJQ/text.txt",
        "/applications/HI/Milk/HEADIAJQ/thisworks!!!!.txt",
        "/applications/HI/MilkiM/TRASHINGTHINGS.txt",
        "/applications/HI/emoji.txt",
        "/applications/Test",
        "/applications/Test/Tjinsg",
    };
    TreeItem<String> root = new TreeItem<>("root");
    for (String path : paths)
        add(root, path);
    print(root, "");

result:

TreeItem: root
  TreeItem: applications
    TreeItem: HI
      TreeItem: Hey.txt
      TreeItem: Milk
        TreeItem: blah
        TreeItem: HEADIAJQ
          TreeItem: text.txt
          TreeItem: thisworks!!!!.txt
      TreeItem: MilkiM
        TreeItem: TRASHINGTHINGS.txt
      TreeItem: emoji.txt
    TreeItem: Test
      TreeItem: Tjinsg
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a class you will need to separate folders from files:

import java.io.File;

import javafx.scene.control.TreeItem;
import javafx.scene.image.ImageView;

public class SystemFileItem extends TreeItem<String> {

    /** Stores the full path to the file or directory */
    private String fullPath;
    private boolean isDirectory;

    /**
     * Constructor
     * 
     * @param path
     */
    public SystemFileItem(String path) {
        super(path);
        this.fullPath = path;

        // test if this is a directory and set the icon
        if (new File(fullPath).isDirectory()) {
            isDirectory = true;
            //setGraphic(new ImageView(SystemFilesTree.folderImage));

        } // if you want different icons for different file types this is
            // where you'd do it
        else {
            isDirectory = false;
            //setGraphic(new ImageView(SystemFilesTree.fileImage));
        }

        // set the value
        if (!fullPath.endsWith(File.separator)) {
            // set the value (which is what is displayed in the tree)
            String value = path;
            int indexOf = value.lastIndexOf(File.separator);
            if (indexOf > 0) {
                this.setValue(value.substring(indexOf + 1));
            } else {
                this.setValue(value);
            }
        }
    }

    public String getFullPath() {
        return fullPath;
    }

    public boolean isDirectory() {
        return isDirectory;
    }
}

The other are pretty much code logic(build a method that is adding files to roots etc):

    SystemFileItem root = new SystemFileItem("Root File");
    TreeView<String> treeView= new TreeView<String>(root);
    libraryTreeView.setEditable(true);
    SystemFileItem treeNode = new SystemFileItem("file path");
   ......

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.