File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Updating a JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Updating a JTree" Watch "Updating a JTree" New topic
Author

Updating a JTree

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi. I have an app that I want to update a JTree as a user enters data. Right now, I have a JTree as the left component on a JSplitPane, and a panel for Maintenance for this JTree as the right component. When the user adds a new entry into the DB, I want the JTree to automatically show the new data after the user presses save.
I am trying to use the reload() method to accomplish this, but am not seeing any changes in the JTree. Could someone tell me what I've missed or done wrong? Here's the method that runs when the user presses Save...


Here's the "refreshTree" method from another class...

I am using the DefaultTreeModel. Let me know if you need to see any other code. Thanks for any help!
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You'll need to rebuild the tree model from the database in the "refreshTree()" method. "reload()" tells the tree model that some of its nodes were changed and it needs to update. Since your nodes aren't actually tied to the database, but only hold data that was pulled from the database at the time they are created, the nodes haven't changed, only the data in the database has. You'll need to rebuild the tree model, just like you did when you first created it.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Thanks for straightening me out. I've got the tree updating now.
However, I do have another question on why my tree is doing something.
Here are some records from the db:
Backsplash Laminated Finished
Backsplash Top No Sub-Group
Backsplash Top Without Doors
Basically, from left to right these values are being added into my tree. If the third column has a value of "No Sub-Group" , I do not want to add this as a node to the value in the second column (as you will see in my code).
My problem is I keep getting two "Top" nodes showing up under my "Backsplash" node, and I don't know why. One of the "Top" nodes has no children, and the other has a child ("Without Doors").Could someone take a look at my code and tell me why?
Thanks again!!

[ April 15, 2003: Message edited by: Jennifer Sohl ]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I think your problem is that the "isNodeChild()" method is not returning what you think it is. For example :


Right before you called "isNodeChild()" you created a new node. So it is impossible for this new node to be a child of the family node! Instead of checking whether a node is a child, you need to see if one of the existing nodes contains the string value you are trying to add.

This would be done like this (modified your code):
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Aha! You were right. isNodeChild() was not doing what I thought it was. The modified code you gave me works wonderfully.
But now I wonder, what does isNodeChild() exactly do, and when would be a situation that I would need to use it?
Thanks again for all of your help!!
I really, really appreciate it.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

isNodeChild() tests to see if the specified node is a child of this one. The node is separate from the value it contains. I haven't tested this out, but I assume that DefaultMutableTreeNode just uses the equals() method to find this out. Default equals() just tests references. You can override equals() to test equality in other ways (for example, test the values of the User Objects contained in the node rather than test node references against each other.), but I'm not sure how this would affect the tree...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Updating a JTree
 
Similar Threads
Help on objects
Reconnecting to Database
Setting the data in a JTable
Statement is closed?
Result Set problems