0

I had some trouble putting up a tree and run it to get a result as this picture below.

enter image description here

So I am supposed to create a tree with roots and children by a .txt file.

This is the code, and I will give a short explanation below.

public class Tree extends TreeFrame{

    Tree() throws FileNotFoundException {
        super();
    }

    private static final long serialVersionUID = 1L;    
    DefaultTreeModel model;
    Nod nod;
    static Tree main;
    static String filen ="";

    public void initTree() throws FileNotFoundException{

        filen = "C:/Users/MyComp/workspace/Lab5/src/Lab5/Life.txt";         
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(new File(filen));                              
        sc.nextLine();                                                          
        ArrayList<DefaultMutableTreeNode> ar = null;

        while (sc.hasNextLine()){
            String sc2 = sc.nextLine().replace("<", "");
            char chr = '/';

            if (sc2.charAt(0) != chr){
                String[] parts = sc2.split(" ");
                String[] parts2 = parts[1].split("=\"");
                String[] parts3 = sc2.split(">");
                nod = new Nod(parts2[1].substring(0, parts2[1].length() - 2), parts[0], parts3[1]);
                ar = new ArrayList<DefaultMutableTreeNode>();           

                for (int i = 0; i < nod.getDepth(); i++){
                    ar.add(nod);                                                
                }
            }

        buildTree(ar);                                                          
        }
    }

    void buildTree(ArrayList<DefaultMutableTreeNode> a){    

            model = new DefaultTreeModel (a);       
            tree = new JTree(model);                            
            tree.setBackground(Color.green);                    
    }


    void showDetails(TreePath path){
        if (path == null)
        return;

        int a = path.getPathCount()-1;
        DefaultMutableTreeNode b = (DefaultMutableTreeNode) path.getPathComponent(a);
        String info = ((Nod) b).getText();
        JOptionPane.showMessageDialog(this, info);
    }


    public static void main(String[] args) throws FileNotFoundException{
        if(args.length>0){
            filen=args[0];
        }

        main = new Tree();                                  
    }
}

Nod is just the class which makes the objects with name, level and text.

So the main thought was to, import the file -- > read it --> make objects of it --> create an array and add the "nodes"/objects in it --> make a TreeModel by the array/"nodes" --> create a tree by the TreeModel (as you see) --> and just run it with the extension to obtain the GUI. But I get syntax error on ArrayList<DefaultMutableTreeNode>.

Do you see the issue?

9
  • Always post the full error message with your question. You want to make the question as easy to answer as possible, no? Commented May 23, 2017 at 22:19
  • Note you're re-creating the arraylist with each iteration of a loop, something that doesn't make sense. Create the arraylist from the beginning and use it but don't keep re-creating one. Commented May 23, 2017 at 22:20
  • I just wanted to add all the objects in same array, but yea I think it is something wrong with that loop. Commented May 23, 2017 at 22:22
  • Again, you're recreating the ArrayList, meaning anything added to the ArrayList earlier in the loop will be thrown out when the new ArrayList replaces the old one. Again, does this make sense? Commented May 23, 2017 at 22:24
  • Also, why are you using ArrayList in the first place when you want to create a tree? An ArrayList is a linear list, not a tree structure. Why not create a JTree instead and a DefaultTreeModel? Commented May 23, 2017 at 22:25

2 Answers 2

1

The immediate problem is that no DefaultTreeModel constructor accepts an ArrayList as an argument. See the docs here.

I think you might need to read this tutorial on how to use JTree.

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

Comments

0

You should change this if clause:

if (sc2.charAt(0) != chr){ String[] parts = sc2.split(" ");
String[] parts2 = parts[1].split("=\"");
String[] parts3 = sc2.split(">");
nod = new Nod(parts2[1].substring(0, parts2[1].length() - 2), parts[0], parts3[1]);
// ar = new ArrayList<DefaultMutableTreeNode>();
for (int i = 0; i < nod.getDepth(); i++){ ar.add(nod); } }

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.