[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

JTree problem

Posted on 2003-03-03
3
Medium Priority
?
292 Views
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?

Thanx,




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();
      trmodel.setRoot(data);
      tree = new JTree(trmodel);
      int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;
      tree.getSelectionModel().setSelectionMode(mode);
   }


   
    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;
      fireTreeStructureChanged(oldRoot);
   }

   public Object getRoot()
   {
      return root;
   }

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

   public Object getChild(Object parent, int index)
   {
   if(parent instanceof MapData)
   return ((MapData)parent).getSymbolCollection(index);
   else
    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);
   }

}
0
Comment
Question by:asukai
  • 2
3 Comments
 
LVL 1

Expert Comment

by:ykaganov
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.
0
 

Author Comment

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

Thanx
0
 
LVL 1

Accepted Solution

by:
ykaganov earned 320 total points
ID: 8068154
inside ObjectTreeModel:

  public void addChild(MapData parent, Object child)
   {
      parent.add(child);    
      fireTreeStructureChanged(parent);
   }

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

- Eugene
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month8 days, 2 hours left to enroll

607 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