Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Updating JTree When Adding Nodes

Thread view: 
Jason Cavett - 25 Jul 2007 17:30 GMT
I'm having an issue updating my JTree when I add a node to the tree.

When I add to the JTree directly, I do the following (this method is
in my class that extends JTree)

public void addNode(DataModel parent, DataModel newChild) {
 // create new node and expand the path to show the node
 ((DataModel) parent).insert(newChild, parent.getChildCount());

 // if a successful addition
 if (newChild.getParent() != null &&
newChild.getParent().equals(parent)) {
  treeModel.addTreeModelListener(newChild);

  // update tree and scenario values
  newChild.updateScenarioModel();

  // make the tree an observer of the DataModel (and the DataModel's
  // children)
  this.addObservers(newChild);

  ((DataModel) newChild.getParent()).sort();
  this.updateUI();
}

DataModel implements MutableTreeNode and the root node is passed in to
the JTree on creation.

So, I'm basically adding everything directly to the JTree.  Honestly,
now that I think about it, that doesn't entirely seem right as the
JTree should be *just* a view.  So, I have been trying, instead, to
add to the node.  However, the JTree doesn't update or refresh when I
add the node.

I've been reading online, and there is a lot of discussion about
TreeModel's, but I am not sure if I entirely understand how this plays
into the TreeNodes or how they work together.

Any further explanation would be welcomed.  Thanks.
Sabine Dinis Blochberger - 26 Jul 2007 09:23 GMT
> I'm having an issue updating my JTree when I add a node to the tree.

When I create a new tree structure, I call
   model.reload();

After changing a child node (setting the user object), I call
   model.nodeChanged(child);

If you use model.insertNodeInto(), you don't need to tell the tree to
update, the method does it already.

Hope this will help.
Signature

Sabine Dinis Blochberger

Op3racional
www.op3racional.eu

Jason Cavett - 01 Aug 2007 19:36 GMT
On Jul 26, 4:23 am, Sabine Dinis Blochberger <no.s...@here.invalid>
wrote:
> > I'm having an issue updating my JTree when I add a node to the tree.
>
[quoted text clipped - 12 lines]
>
> Op3racionalwww.op3racional.eu

That will work if I have access to the JTree's model.  But, currently,
I only have access to the nodes that makeup the JTree (DataModel).
The DataModel != TreeModel.

Because DataModel implements MutableTreeNode, it does have an "insert"
method where I can add a child to the DataModel.  It seems to me that
there should be some way for the DataModel (as a node) to let its Tree
know that it had a child added to it.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.