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

ListgetLastExpandedNodes(final JTree tree)
get Last Expanded Nodes
final List<DefaultMutableTreeNode> nodeList = new ArrayList<DefaultMutableTreeNode>();
final int rowCount = tree.getRowCount();
for (int i = 0; i < rowCount; i++) {
    final TreePath path = tree.getPathForRow(i);
    DefaultMutableTreeNode lastPathComponent = null;
    try {
        lastPathComponent = (DefaultMutableTreeNode) path.getLastPathComponent();
    } catch (final Exception e) {
...
TreePath[]getPaths(JTree tree, boolean expanded)
Permet de convertir un TreeNode en TreePath[]
TreeNode root = (TreeNode) tree.getModel().getRoot();
List<TreePath> list = new ArrayList<>();
getPaths(tree, new TreePath(root), expanded, list);
return (TreePath[]) list.toArray(new TreePath[list.size()]);
IcongetTreeSelectedExpandedIcon()
get Tree Selected Expanded Icon
return getTreeExpandedIcon();
booleanisTreePathExpandable(TreeModel treeModel, TreePath treePath)
is Tree Path Expandable
if (treeModel == null)
    return false;
if (treePath == null)
    return false;
return !treeModel.isLeaf(treePath.getLastPathComponent()) && isTreePathInModel(treeModel, treePath);
voidmakeTreeAutoExpandable(JTree tree)
By calling this method, the provided tree will become auto-expandable, i.e.
makeTreeAutoExpandable(tree, 1, false);
booleansearchUnexpandedPath(JTree tree, TreePath path, int index, TreeNode node, TreePath[] result, boolean compareOnlyLabels)
search Unexpanded Path
Object[] pathElements = path.getPath();
if (index >= pathElements.length) {
    return !tree.isExpanded(result[0]);
Object elementToSearch = pathElements[index];
Enumeration<?> enumeration = node.children();
while (enumeration.hasMoreElements()) {
    TreeNode next = (TreeNode) enumeration.nextElement();
...
voidsetExpandedIcon(JTree tree, Icon icon)
Set's the expanded icon to use for the given JTree if that tree's UI delegate extends from BasicTreeUI .
((BasicTreeUI) tree.getUI()).setExpandedIcon(icon);
voidsetExpandedOnEdt(JTree tree, TreePath path, boolean expanded)
set Expanded On Edt
if (expanded) {
    expandPathOnEdt(tree, path);
} else {
    collapsePathOnEdt(tree, path);
voidsetExpandedPaths(JTree tree, TreePath[] expandedPaths)
Expand all the previously remembered expanded paths.
if (expandedPaths == null) {
    return;
for (int i = 0; i < expandedPaths.length; ++i) {
    TreePath oldPath = expandedPaths[i];
    TreePath newPath = searchPath(tree.getModel(), oldPath);
    if (newPath != null) {
        tree.expandPath(newPath);
...
voidsetFullyExpandedPath(JTree Target, TreeModel TheModel, Object Node, Vector Path)
set Fully Expanded Path
if (TheModel.isLeaf(Node) || TheModel.getChildCount(Node) == 0) {
    Object[] FullPath = new Object[Path.size()];
    Path.copyInto(FullPath);
    Target.expandPath(new TreePath(FullPath));
    return;
for (int i = 0; i < TheModel.getChildCount(Node); i++) {
    Object Child = TheModel.getChild(Node, i);
...