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 / GUI / September 2005

Tip: Looking for answers? Try searching our database.

howto update JTree visually

Thread view: 
William Z. - 07 Sep 2005 17:28 GMT
I'm trying to update a JTree after it's already been created, so after
a few operations, I add more nodes to my root node and would like to
update it in my scrollpane. My problem is, when I add nodes to it, I
don't know how to make it visually update.

I have code like this that get's dynamically appended to throughout my
program.

---

DefaultMutableTreeNode books = new DefaultMutableTreeNode("books");

for (int i = 0; i < books.size(); i++) {
    books.add(new DefaultMutableTreeNode("Book: " + i));
}

root.add(books);
// now how to make the JTree visually update in the scrollpane

---

Any help much appreciated.
Pete Barrett - 07 Sep 2005 18:43 GMT
>I'm trying to update a JTree after it's already been created, so after
>a few operations, I add more nodes to my root node and would like to
[quoted text clipped - 14 lines]
>root.add(books);
>// now how to make the JTree visually update in the scrollpane

revalidate() usually works for me.  Also available are 2 repaintS, 2
paintImmedatelyS, and probably others as well!

Pete Barrett
Roland - 07 Sep 2005 19:25 GMT
> I'm trying to update a JTree after it's already been created, so after
> a few operations, I add more nodes to my root node and would like to
[quoted text clipped - 18 lines]
>
> Any help much appreciated.

Use the tree's model; by default, JTree's TreeModel is an instance of
DefaultTreeModel.
You can use DefaultTreeModel.insertNodeInto for each node separately
(order is important, so you'll have to move the code to add 'books' to
'root' to just *before* the for-loop), or use
DefaultTreeModel.nodeStructureChanged or
DefaultTreeModel.nodesWereInserted after adding a batch of nodes.

<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/tree/DefaultTreeModel.html>
<http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#dynamic>
Signature

Regards,

Roland de Ruiter
` ___      ___
`/__/ w_/ /__/
/  \ /_/ /  \

Babu Kalakrishnan - 07 Sep 2005 19:38 GMT
> I'm trying to update a JTree after it's already been created, so after
> a few operations, I add more nodes to my root node and would like to
> update it in my scrollpane. My problem is, when I add nodes to it, I
> don't know how to make it visually update.

The preferred way to mutate the tree structure is to use methods
provided in the model to do so (Look at the API docs of DefaultTreeModel
: insertNodeInto method). If for some reason you cannot do this and need
to add nodes directly, you would need to inform the model of these
changes by calling its notification methods (nodesWereInserted,and other
similar named ones).

BK
Roedy Green - 10 Sep 2005 12:16 GMT
>I'm trying to update a JTree after it's already been created, so after
>a few operations, I add more nodes to my root node and would like to
>update it in my scrollpane. My problem is, when I add nodes to it, I
>don't know how to make it visually update.

The basic trick is code like this:

  ConstellationTreeModel.model.fireTreeStructureChanged(
ConstellationTreeModel.root , new TreePath ( new Object[] {
ConstellationTreeModel.root} ));


You update your model, then fire the appropriate change event. Swing
will then arrange to repaint the affected nodes.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



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.