0

I have a

Class Category 
private Category parent;
private String name;
private ArrayList<Category> categories;
private DefaultMutableTreeNode node;

and an

ArrayList<Category> categories

the ArrayList contains

[0]Categorie #1
   parent = null
   name = asdasd
   ArrayList<Category> categories
      [0]Categorie #2
          parent = #1
          name = asdasddsa
          ArrayList<Category> categories
              [0]Categories #4
                  parent = #2 
                  name = asdasdasd
                  ArrayList<Category> categories = null
      [1]Categorie #3
          parent = #1
          name = asdasdasdasdasd
          ArrayList<Category> categories = null

and i want to create a JTree out of that ArrayList. I tried it with a function that uses itself until the categories ArrayList was null but with this method i couldn't have more than 1 category in the parent categorie.

I created these functions but they don't work.

public void generateNodes(Category category,DefaultMutableTreeNode root){
    ArrayList<Use> usages = category.getUsages();
    for(Use use: usages){
        category.getNode().add(use.getNode());
    }
    if(!category.getCategories().isEmpty()){
        root.add(category.getNode());

        for(Category cat: category.getCategories()){
            generateNodes(cat, category.getNode());
        }

    }else{
        root.add(category.getNode());
    }

}

1 Answer 1

1

You just need to create a recursive method. Every Category you come across, create a DefaultMutableTreeNode, then add the properties as child nodes. Then recursively call method with the contained Cateogories.

If you want to have more than one category as the root parent, you should make a 'fake' root node, then call this on JTree :

setRootVisible(false);

Its still one tree, but it looks like many. Is that what you meant?

Your code should look something like this :

public void generateNodes(Category categories,DefaultMutableTreeNode root){
        ArrayList<Use> usages = categ.getUsages();
        for(Use use: usages){
            categ.getNode().add(use.getNode());
        }
        if(!categ.getCategories().isEmpty()){
            root.add(categ.getNode());

            for(Category cat: categ.getCategories(){
                generateNodes(categ, root);
            }

        }else{
            root.add(categ.getNode());
        }

}
Sign up to request clarification or add additional context in comments.

7 Comments

i added the functions i created if you want to take a look at them
you need to pass in root into the second method as well : private void generateNodes(ArrayList<Category> categories, root)
the root needs to go into the second message, so that it can get any DefaultMutableTreeNodes that are created in the recursive method
actually you only need the first method, and its signature should be : public void generateNodes(Category categorie,DefaultMutableTreeNode root){
I added code to my original answer. You need ONE method not the TWO. And it should look like my example (I dont have IDE so i cant check it)
|

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.