code: public static void collapseAll(JTree tree, boolean omitRoot)
      {
          int rows = tree.getRowCount();
          int limit = (omitRoot ? 1 : 0);
          for (int i = rows - 1; i >= limit; i--)
          {
              tree.collapseRow(i);
          }
      }
docs: Collapse all rows of the given tree
  
  @param tree The tree
  @param omitRoot Whether the root node should not be collapsed