get JTree Expansion State - Java Swing

Java examples for Swing:JTree

Description

get JTree Expansion State

Demo Code


//package com.java2s;

import javax.swing.JTree;
import javax.swing.tree.TreePath;

public class Main {
    /**//ww w  .  j  av a2  s.  co  m
     *
     *
     * @param tree
     * @param row
     *
     * @return
     */
    public static String getExpansionState(JTree tree, int row) {
        TreePath rowPath = tree.getPathForRow(row);
        StringBuffer buf = new StringBuffer();
        int rowCount = tree.getRowCount();

        for (int i = row; i < rowCount; i++) {
            TreePath path = tree.getPathForRow(i);

            if ((i == row) || isDescendant(path, rowPath)) {
                if (tree.isExpanded(path)) {
                    buf.append("," + String.valueOf(i - row));
                }
            } else {
                break;
            }
        }

        return buf.toString();
    }

    /**
     *
     *
     * @param path1
     * @param path2
     *
     * @return
     */
    public static boolean isDescendant(TreePath path1, TreePath path2) {
        int count1 = path1.getPathCount();
        int count2 = path2.getPathCount();

        if (count1 <= count2) {
            return false;
        }

        while (count1 != count2) {
            path1 = path1.getParentPath();

            count1--;
        }

        return path1.equals(path2);
    }
}

Related Tutorials