Solved

JTree Refresh

Posted on 2008-10-07
5
1,014 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse java  build path 6 56
eclipse shortcuts 9 54
eclipse apache tomcat admin console 52 96
Java: How do I open the default windows program for a given file type 3 28
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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

775 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