AndreiRS AndreiRS - 1 month ago 26
Java Question

Java SWT Tree Expand All / Collapse All

How can I make an Expand All and a Collapse All functionality for my own SWT Tree, similar to the Collapse All button in Eclipse's Package Explorer?

Answer

If you are just using the SWT Tree you must loop through all the items in the tree and expand / collapse each one. For example:

boolean expanded = true to expand, false to collapse

Tree tree = ... your tree

tree.setRedraw(false);  // Stop redraw until operation complete

TreeItem [] items = tree.getItems();

for (TreeItem item : items) {
   item.setExpanded(expanded);
}

tree.setRedraw(true);

If you are using the JFace TreeViewer call the viewer collapseAll or expandAll methods.