简体   繁体   中英

How to show only root in JTree?

I have a JTree like this:

private TreeNode createNodes() {

        DefaultMutableTreeNode root;
        DefaultMutableTreeNode grandparent;
        DefaultMutableTreeNode parent;

        root = new DefaultMutableTreeNode("San Francisco");

        grandparent = new DefaultMutableTreeNode("Potrero Hill");
        root.add(grandparent);

        parent = new DefaultMutableTreeNode("Restaurants");
        grandparent.add(parent);

        dummyParent = root;

        return root;
    }

I want to show only the root "San Francisco" in the screen not by default. Then the tree only expands when I click to the extended icon. How can I do that? I had tried to use some methods:

         tree.setRootVisible(true);
         tree.setShowsRootHandles(false);

I want to show only the root "San Francisco" in the screen

After you have created the data and added the model to the tree you can use:

tree.collapseRow(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM