Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JTree Refresh

Posted on 2008-10-07
5
Medium Priority
?
1,043 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

688 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