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 / June 2006

Tip: Looking for answers? Try searching our database.

JTree model update problem

Thread view: 
Duane Evenson - 10 Jun 2006 19:03 GMT
Forgive the newbe level of this question.
I have define and declare a root node, jtree and model. I add nodes to the
model. The tree doesn't get updated.
Why?
I thought insertNodeInto fired tree updates.
I have to use model.reload() to update the tree.

TIA
Duane

Here's my test code:
//-------------------------------------
import javax.swing.*;
import javax.swing.tree.*;

public class TestTree {
    public static void main(String[] args) {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("top");
        JTree tree = new JTree(root);
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
       
        for (int x = 1; x <= 10; x++) {
            model.insertNodeInto(new DefaultMutableTreeNode("node " + x),
                    root, root.getChildCount());
        }
       
        //model.reload();
        JFrame frame = new JFrame();
        frame.add(tree);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
m.erxleben@web.de - 10 Jun 2006 19:39 GMT
Hello,

the way to use the JTree is to add it into a JScrollPane
The code for this is:

JScrollPane scrollPane = new JScrollPane();
JViewPort viewPort = scrollPane.getViewport();
viewPort.add(tree);

and instead add(tree) use add(scrollPane);

Just my 0.02c

Martin
Thomas Hawtin - 10 Jun 2006 19:50 GMT
> I have define and declare a root node, jtree and model. I add nodes to the
> model. The tree doesn't get updated.

The tree gets updated. The problem is that a branch with no child nodes
will be closed. You need to open the branch after adding a node to it.
This is particularly annoying if you are modifying a tree, and a branch
only temporarily has no children.

>         DefaultMutableTreeNode root = new DefaultMutableTreeNode("top");
>         JTree tree = new JTree(root);
[quoted text clipped - 4 lines]
>                     root, root.getChildCount());
>         }

                tree.expandRow(0);

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Duane Evenson - 10 Jun 2006 20:15 GMT
>> I have define and declare a root node, jtree and model. I add nodes to the
>> model. The tree doesn't get updated.
[quoted text clipped - 3 lines]
> This is particularly annoying if you are modifying a tree, and a branch
> only temporarily has no children.

Thanks!
Silly me, I clicked on the root node and everything showed up OK.

>>         DefaultMutableTreeNode root = new DefaultMutableTreeNode("top");
>>         JTree tree = new JTree(root);
[quoted text clipped - 8 lines]
>
> Tom Hawtin


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.