如何在 JTree 中只显示根目录?

How to show only root in JTree?

我有一个 JTree 这样的:

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;
    }

我只想在屏幕上显示根目录 "San Francisco",不是默认情况。然后只有当我点击扩展图标时树才会扩展。我怎样才能做到这一点?我曾尝试使用一些方法:

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

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

创建数据并将模型添加到树后,您可以使用:

tree.collapseRow(0);