Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JTree Refresh

Posted on 2008-10-07
5
Medium Priority
?
1,046 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

885 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