Link to home
Start Free TrialLog in
Avatar of Cmerf486
Cmerf486

asked on

JTree Refresh

I am using a JTree in my application that needs to be abel to have nodes added to it and removed from it dynamically. Using the following function, everything works, but after one node has been added, any subsequent nodes I add don't show up in the JTree. There are no Exceptions thrown and I can reference the node programmatically. Is there something special I need to do to update the display? Any help would be appreciated. Thank you.
public void addTransition( Transition transition ) 
    {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode( transition ); 
        
        try 
        {
            transitionNode.add( node ); 
            treLayoutTree.scrollPathToVisible( new TreePath( node.getPath() ) );
        }
        catch ( Exception e ) 
        {
            Main.getLogger().writeToLogWarn( "MainWindow.addTransition() Exception: " + e.getMessage() ); 
        }
    }

Open in new window

Avatar of zzynx
zzynx
Flag of Belgium image

DefaultTreeModel has a method nodeStructureChanged() that might be useful in your case:

    /**
      * Invoke this method if you've totally changed the children of
      * node and its childrens children...  This will post a
      * treeStructureChanged event.
      */
    public void nodeStructureChanged(TreeNode node)

So, add the nodes you want.
Then perform the above method on the parent node
ASKER CERTIFIED SOLUTION
Avatar of MicheleMarcon
MicheleMarcon
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(I edited before zzynx comment was showed... so I wasn't referring to him/her)
Avatar of Cmerf486
Cmerf486

ASKER

Thanks a lot!
zznyx,
Thanks for your entry and help!

MicheleMarcon,
Yours proved to be an easy solution, thanks. Here is my new code for anyone who might run into the same problem.
    public void addTransition( Transition transition ) 
    {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode( transition ); 
        
        try 
        {
            ((DefaultTreeModel)(treLayoutTree.getModel())).insertNodeInto( node, transitionNode, transitionNode.getChildCount() );
        }
        catch ( Exception e ) 
        {
            Main.getLogger().writeToLogWarn( "MainWindow.addTransition() Exception: " + e.getMessage() ); 
        } 
    }

Open in new window