Problem with JTree - Changing the model

Last time I had to work with the JTree class, I decided that it was one of the worst parts of Swing, and as of my current project, my mind is unchanged :stuck_out_tongue: When the UI is initialized, there are no game objects in the world yet so the tree is empty (constructed using a null root node). So, when the root object is added I need to add a root node for it and update the tree:

    public void create(final MyTreeNode root)
    {
        rootNode = root;
        model = new MyTreeModel(root); //Inherits DefaultTreeModel; currently doesn't override anything
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                setModel(model);
                model.reload(); //I'm not sure if both of these are needed
                model.nodeStructureChanged(root);
            }
        });
    }

But, this has no effect–the tree is still blank. I found some stuff about a treeStructureChanged() event in the TreeModelListener, but I couldn’t find a way to fire it. How do I change the model?

Might be cause it’s in a run thread?

Don’t change the model of the tree, just keep changing the root node. Keep a reference to your custom DefaultTreeModel subclass, and in your SwingUtilities.invokeLater(), simply call model.setRoot(newRoot). The model then handles the fireTreeStructureChanged() calls as appropriate.

That didn’t work at first, but I figured it out–somehow, I had one instance of the tree that was being shown on the screen, and another instance that create() was being called on (and the nodes being added to.)