Solved

JTree Refresh

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
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

617 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