• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 745
  • Last Modified:

Adding nodes to a JTree and refreshing

I have an application with a JTree component,

however, the Jtree model is set from an adapter which creates the nodes from a database.

Now my problem is how can i get the Jtree to refresh itself if the info in the database changes, i.e. more nodes added?

What i am currently doing is creating a new instance of the model, as reload has no effect. If i do reload and also refresh the info in the adpater the Jtree does not recognise that nodes have been added and thus not reflected on the JTree component,

can anyone help?
  • 8
  • 5
  • 2
1 Solution
have you tried revalidate() of the jtree?
plaskowjAuthor Commented:
what does revalidate actaully do?

Support for deferred automatic layout.

Calls invalidate() and then adds this components validateRoot to a list of components that need to be validated. Validation will occur after all currently pending events have been dispatched. In other words after this method is called, the first validateRoot (if any) found when walking up the containment hierarchy of this component will be validated. By default, JRootPane, JScrollPane, and JTextField return true from isValidateRoot().

This method will automatically be called on this component when a property value changes such that size, location, or internal layout of this component has been affected. This automatic updating differs from the AWT because programs generally no longer need to invoke validate() to get the contents of the GUI to update.

See Also:

Component.invalidate(), Container.validate(), isValidateRoot(), RepaintManager.addInvalidComponent(javax.swing.JComponent)


The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

plaskowjAuthor Commented:
Ok so here we go

I have created a adapter for the model
inside this adpater i reload the information(method called refreshme so my refresh method the tree looks like this

 data.refreshMe();//here as the db is updated new nodes are added

//but nothing happens
'don't mislead the man' :))

this is a classic model-view situation.
when model detects some changes in the data, it should inform the view.
In Swing this functionality is implemented during standard EventSource -> EventListener pattern.

DefaultTreeModel has the appropriate helper methods

if you implement your own TreeModel, you should handle all the Listener regiostering and notification yourself ..

and when your model detect changes, it should call the appropriate method on all the registered listeners

Invoked after a node (or a set of siblings) has changed in some way.

best regards
plaskowjAuthor Commented:
excellent, do you an example of this

many thanks
if you need more informatin, you can take a look at the source of
DefaultTreeModel to see how inserting / removing nodes is implemented (DefaultTreeModel fires the appropriate event to all its listeners on such a change)
plaskowjAuthor Commented:
I have added the action listeners to my model (newModel)

 newModel.addTreeModelListener(new com.sun.java.swing.event.TreeModelListener() {
      public void treeNodesChanged(TreeModelEvent e) {
      public void treeNodesInserted(TreeModelEvent e) {
      public void treeNodesRemoved(TreeModelEvent e) {
      public void treeStructureChanged(TreeModelEvent e) {

but how for examples do i get the newModel_treeNodesInserted(e)
method to load up the new code, taking into account thye nodes are made up of database records

can you help, please

you need to implement these two methods in your TreeModel to handle registering of TreeModel listeners (TreeModelUI is one og them)

    public void addTreeModelListener(TreeModelListener l) {
        listenerList.add(TreeModelListener.class, l);

    public void removeTreeModelListener(TreeModelListener l) {
        listenerList.remove(TreeModelListener.class, l);

then you can make some helper method that creates event and notifies all listeners.

protected void fireTreeStructureChanged(Object source, Object[] path,
                                    int[] childIndices,
                                    Object[] children) {
    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    TreeModelEvent e = null;
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
        if (listeners[i]==TreeModelListener.class) {
            // Lazily create the event:
            if (e == null)
                e = new TreeModelEvent(source, path,
                                       childIndices, children);

another helper method
  * 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) {
    if(node != null) {
       fireTreeStructureChanged(this, getPathToRoot(node), null, null);

and then call nodeStructureChanged() method when you change the three structure at some node.

probably your
should call
nodeStructureChange(theRoot) after the reloading ...

take a look at

DefaultTreeModel.java for implementation techniques
DefaultTreeModel is a good TreeModel, because it informs all the listeners after each tree structure change ...

plaskowjAuthor Commented:
do you mean the resfresh method should be

  tree = new JTree();
  newModel = new DefaultTreeModel(data.top);//data is the adapter

   data.refreshMe(); //refresh a method inside the model,
                       //which loads new info from db

plaskowjAuthor Commented:
i don't want listeners, i just want the tree to update on refresham i doing this right?
see above
plaskowjAuthor Commented:
data is just a class which gathers the info for nodes from a database
can you explain waht TreeModel you use (DefaultTreeModel) and what functionality is implemented inside you 'data' adapter...
plaskowjAuthor Commented:
this is the tree model

DefaultTreeModel newModel = new DefaultTreeModel(data.top);

data is the instance of a class which makes up the nodes from a db

top is the root,

The only functionality in data is that of creating a Jtree from a db
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now