private static void expandPath(JTree tree, TreePath treePath, int currentDepth,
			TaskMonitor monitor) throws CancelledException {

		if (currentDepth <= 0) {
			return;
		}

		GTreeNode treeNode = (GTreeNode) treePath.getLastPathComponent();
		TreeModel treeModel = tree.getModel();
		int childCount = treeModel.getChildCount(treeNode);

		if (childCount > 0) {
			for (int i = 0; i < childCount; i++) {
				monitor.checkCancelled();

				GTreeNode n = (GTreeNode) treeModel.getChild(treeNode, i);
				TreePath path = treePath.pathByAddingChild(n);

				expandPath(tree, path, currentDepth - 1, monitor);
			}
		}

		tree.expandPath(treePath);
	}
A GTree task to fully expand a tree node to a maximal depth.
Ghidra/Framework/Docking/src/main/java/docking/widgets/tree/tasks/GTreeExpandNodeToDepthTask.java