tags:

views:

554

answers:

1

I have a question about how to dynamically generate JTrees. Is there a way to set the Root Node invisible without making its children invisible too? I have tried to do the following but it shows all nodes as invisible. Keep in mind that I want to add and remove children of the Root Node at any point in time. I've added comments so you can follow what I intend to do. Let me know if they are doing something I dont need, as I am new to JTrees and don't know the conventions. I would also like to be able to select multiple children for the listener.

    DefaultMutableTreeNode rootNode;
    rootNode = new DefaultMutableTreeNode(); //I want this invisible.

    DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
    JTree tree = new JTree(treeModel);

    treeModel.addTreeModelListener(this);
    tree.setRootVisible(false); // Sets everything invisible
    tree.setEditable(true); //makes tree dynamic
    tree.setShowsRootHandles(true); //supposedly allows you to see the children of the nodes.

    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 
    //I would like the line above to be multi-select; however, this doesn't seem to be an option.

    DefaultMutableTreeNode table = new DefaultMutableTreeNode( "table1");
    rootNode.add(book);

    DefaultMutableTreeNode value = new DefaultMutableTreeNode( "value");
    table.add(value);

In the above example. Nothing is shown and when I remove the "tree.setRootVisible(false)" everything is visible including the node.

+1  A: 

Works fine for me. I based my tests on the TreeDemo from the Swing tutorial on How to Use Trees. Compare your code with the tutorial code to see what the difference is.

camickr
When I run the demo I see "The Java Serires" and its two childen. When I add tree.setRootVisible(false) I only see the two children.
camickr
I don't understand the comment because that is what happens as I explained above when you use the setRootVisible(...) method?
camickr
I guess something in my code was missing, you're right it works fine in the tutorial. I'll have to see what I'm doing differently in my code :)
Jeffrey