JTree problem

Posted on 2003-03-03
Medium Priority
Last Modified: 2008-02-01
I have a JTree, custom TreeModel which implements TreeModel interface and a custom hierarchical data sturucture object. At the moment when my program modifies the hierarchical data , the JTree does not get updated. Can anyone please tell me how to solve this?


The following code is a fragment of my code.

class TreeProblem

   private JTree tree;
private MapData data; //Custom hierarchical  data

   public TreeProblem()

      // the root of the class tree is Object
      ObjectTreeModel trmodel = new ObjectTreeModel();
      tree = new JTree(trmodel);
      int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;

    public void updatePanel()
    data.add("TEST"); //Updating the custom hierarchical data.

class ObjectTreeModel implements TreeModel

   private MapData root;
   private EventListenerList listenerList
      = new EventListenerList();
   public ObjectTreeModel()

      Sets the root to a given variable.
      @param v the variable that is being described by this tree
   public void setRoot(MapData v)
MapData oldRoot = v;
      root = v;

   public Object getRoot()
      return root;

   public int getChildCount(Object parent)
   if(parent instanceof MapData)
   return ((MapData)parent).getSymbolCollectionSize();
      return ((SymbolCollection)parent).getSize();

   public Object getChild(Object parent, int index)
   if(parent instanceof MapData)
   return ((MapData)parent).getSymbolCollection(index);
    return ((SymbolCollection)parent).get(index);


   public int getIndexOfChild(Object parent, Object child)
      int n = getChildCount(parent);
      for (int i = 0; i < n; i++)
         if (getChild(parent, i).equals(child))
            return i;
      return -1;

   public boolean isLeaf(Object node)
      return node instanceof ObjectMC;

   public void valueForPathChanged(TreePath path,
      Object newValue)

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

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

Question by:asukai
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

Expert Comment

ID: 8060258
When you change the hierarchy in MapData in your updatePanel method, you don't propagate that change to the Model anywhere. So, the Model doesn't know that anything changed and doesn't notify the tree.

You should either rewrite updatePanel to go through a Model API (which would call data.add() and also fire a model change event), or have the MapData objects fire events to which the model can subscribe.

Author Comment

ID: 8063288
-> rewrite updatePanel to go through a Model API (which would call data.add() and also fire a model change event),

I am trying to do this but don't know how. Could you please tell me specifically how to do this? some pice of code would be very helpful.


Accepted Solution

ykaganov earned 320 total points
ID: 8068154
inside ObjectTreeModel:

  public void addChild(MapData parent, Object child)

In your code, never add child to MapData directly, always go through the model.dadChild()

- Eugene

Featured Post

Industry Leaders: 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

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 how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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