AndreiRS AndreiRS - 5 months ago 77
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?


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) {


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