# When I use JTree, by default it is in a collapsed state. How can I make it initially expanded?

Sandip Chitale

I think the first level is always expanded.

To have every level expanded you could use -

```
public void expandPath(TreePath path)
Ensures that the node identified by the specified path is expanded and viewable.
Parameters:
path - the TreePath identifying a node

```
of javax.swing.JTree.

The trick is to compute the TreePaths. The general algorithm would be to compute the leaf nodes and force expansion of each of the leaf nodes unique parent.

If your tree is made out of a DefaultMutableTreeNodes then you can get the enumeration of all the nodes and thus compute the TreePaths easily.

Comment and Contribute

(Maximum characters: 1200). You have 1200 characters left.