Java Utililty Methods JTree Expand

List of utility methods to do JTree Expand

Description

The list of methods to do JTree Expand are organized into topic(s).

Method

voidautoExpand(JTree tree, TreePath path, int maxLines, int maxChildToExpand, boolean dontExpandToLeafs)
Checks give TreePath for the last node, and if it ends with a node with just one child, it keeps expanding further.
TreeModel model = tree.getModel();
Object node = path.getLastPathComponent();
TreePath newPath = path;
int currentLines = 0;
while (currentLines++ < maxLines && !model.isLeaf(node) && (model.getChildCount(node) > 0)
        && (model.getChildCount(node) <= maxChildToExpand)) {
    for (int i = 0; i < model.getChildCount(node); i++) {
        node = tree.getModel().getChild(node, i);
...
TreePathbuildTreePath(JTree tree, TreePath parent, String[] nodes, int startdepth, boolean expandable)
build Tree Path
TreeNode node = (TreeNode) parent.getLastPathComponent();
String o = node.toString();
if (o.equals(nodes[startdepth])) {
    if (startdepth == nodes.length - 1) {
        return parent;
    if (node.getChildCount() >= 0) {
        for (Enumeration e = node.children(); e.hasMoreElements();) {
...
voidexpand(JTree tree, int depth)
expand
for (int row = 0; row < tree.getRowCount(); row++) {
    if (tree.getPathForRow(row).getPathCount() < depth + 1) {
        tree.expandRow(row);
voidexpand(JTree tree, int level)
expand
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) tree.getModel().getRoot();
while (currentNode != null) {
    if (currentNode.getLevel() <= level) {
        tree.expandPath(new TreePath(currentNode.getPath()));
    currentNode = currentNode.getNextNode();
voidexpand(JTree tree, int startingIndex, int rowCount)
Stolen from http://stackoverflow.com/a/19935987/5620200
for (int i = startingIndex; i < rowCount; ++i) {
    tree.expandRow(i);
if (tree.getRowCount() != rowCount) {
    expand(tree, rowCount, tree.getRowCount());
voidexpandAll(final JTree tree, final TreePath parent, final boolean expand)
expand All
final TreeNode node = (TreeNode) parent.getLastPathComponent();
if (node.getChildCount() >= 0) {
    for (final Enumeration<?> e = node.children(); e.hasMoreElements();) {
        final TreeNode n = (TreeNode) e.nextElement();
        final TreePath path = parent.pathByAddingChild(n);
        expandAll(tree, path, expand);
if (expand) {
    tree.expandPath(parent);
} else {
    tree.collapsePath(parent);
voidexpandAll(JTree jTree)
expand All
for (int i = 0; i < jTree.getRowCount(); i++) {
    jTree.expandRow(i);
voidexpandAll(JTree rightTree)
expand All
expandAll(rightTree, new TreePath(((DefaultMutableTreeNode) rightTree.getModel().getRoot()).getPath()));
voidexpandAll(JTree t)
expand All
expandAll(t, new TreePath(t.getModel().getRoot()));
voidexpandAll(JTree tree)
expand All
tree.expandPath(new TreePath(tree.getModel().getRoot()));
int oldRowCount = 0;
do {
    int rowCount = tree.getRowCount();
    if (rowCount == oldRowCount)
        break;
    oldRowCount = rowCount;
    for (int i = 0; i < rowCount; i++) {
...