Alwe17
Alwe17

Reputation: 25

Tree and Arrays

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?

Upvotes: 0

Views: 45

Answers (2)

A.Bau
A.Bau

Reputation: 82

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); } }

Upvotes: 0

Kevin Anderson
Kevin Anderson

Reputation: 4592

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.

Upvotes: 1

Related Questions