javaswingjtreetreemodel

Dynamic initialization of JTree


i've been banging my head against a brick wall for days when it comes to JTree!

I want to create a JTree which gets its content dynamically at run time, from mp3 tags. So far so good, except im having a massive headache when it comes to populating the JTree. After a lot of experimentation ive finally given up and come for help. Ive been using the class I created below to experiment with ways of adding child nodes to already existing nodes within a JTree. The problem im encountering is within the try block, with the types being incompatible. Error im recieving is:

error: incompatible types: `TreeNode` cannot be converted to `MutableTreeNode`

treeModel.insertNodeInto requires MutableTreeNode for its second argument. The method im using to get the parent node for the second argument, rootNode.getChildAt() returns TreeNode.

Is there anyway around this??? And does anybody now why getChildAt() returns a TreeNode object rather than DefaulMutableTreeNode, the object type rootNode is initialized as???

package jtreeex;

import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author david
 */
public class JTreeEx implements Runnable
{

    public JTreeEx()
    {
        JFrame jfrm = new JFrame();  
        JPanel panel = new JPanel();
        JTree tree;

        DefaultMutableTreeNode rootNode;
        rootNode = new DefaultMutableTreeNode("Audio");
        DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
        treeModel.addTreeModelListener(new MyTreeModelListener());

        tree = new JTree(treeModel);
        tree.setEditable(true);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.setShowsRootHandles(true);
        String [] anArray = {"Dogs", "Cats"};


        // Inserts new root nodes from anArray
        for(int i = 0; i < anArray.length; i++)
        {
            treeModel.insertNodeInto(new DefaultMutableTreeNode(anArray[i]), 
                    rootNode, rootNode.getChildCount());

        }

        // Get enumeration of children of rootNode
        Enumeration eNum = rootNode.children();

        try
        {
            while(eNum.hasMoreElements() == true)
            {
                treeModel.insertNodeInto(new DefaultMutableTreeNode("Toby"), 
                        rootNode.getChildAt(1), rootNode.getChildCount());
            }
        }
        catch (Exception e)
        {
            System.out.println("no enums left");
        }

        panel.add(tree);

        jfrm.add(panel);
        jfrm.setSize(400,200);
        jfrm.setVisible(true);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        (new Thread(new JTreeEx())).start();
    }

    public void run()
    {

    }

}

Solution

  • treeModel.insertNodeInto requires MutableTreeNode for its second argument. The method im using to get the parent node for the second argument, rootNode.getChildAt() returns TreeNode.

    You are building the tree and you know that you are inserting DefaultMutableTreeNodes into the tree so just cast the TreeNode to a DefaultMutableTreeNode:

    treeModel.insertNodeInto(new DefaultMutableTreeNode("Toby"), 
        (DefaultMutableTreeNode)rootNode.getChildAt(1), rootNode.getChildCount());