Solved

JTree Refresh

Posted on 2008-10-07
5
997 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:Cmerf486
  • 2
  • 2
5 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 22666916
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
0
 
LVL 13

Accepted Solution

by:
MicheleMarcon earned 500 total points
ID: 22666952

Your add() does not send the event for tree model changed, so Jtree doesn't know that the model has changed.

(I personally have wasted a lot of time because of this!)

You must use

DefaultTreeModel.insertNodeInto()

;)
0
 
LVL 13

Expert Comment

by:MicheleMarcon
ID: 22666964
(I edited before zzynx comment was showed... so I wasn't referring to him/her)
0
 

Author Closing Comment

by:Cmerf486
ID: 31623597
Thanks a lot!
0
 

Author Comment

by:Cmerf486
ID: 22668686
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

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now