Using Trees with Other Widgets

While Tree is, in and of itself, a massively useful construct, it gains power when used in conjunction with the List widget. Windows Explorer itself is one of these half tree-half list constructs, where the tree view on the left drives the contents of the list on the right.

How do I do that?

Build the same type of interface in the SWT using FillLayout with a Tree and List. Example 13-2 demonstrates the techniques required.

Example 13-2. Building an Explorer-style interface

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.*; import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.*; public class TreeShellExample { Display d; Shell s; TreeShellExample( ) { d = new Display( ); s = new Shell(d); s.setSize(250,200); s.setImage(new Image(d, "c:\\icons\\JavaCup.ico")); s.setText("A Tree Shell Example"); s.setLayout(new FillLayout(SWT.HORIZONTAL)); final Tree t = new Tree(s, SWT.SINGLE | SWT.BORDER); final TreeItem child1 = new TreeItem(t, SWT.NONE, 0); child1.setText("1"); child1.setImage(new Image(d, "c:\\icons\\folder.gif")); final TreeItem child2 = new TreeItem(t, SWT.NONE, 1); child2.setText("2"); child2.setImage(new Image(d, "c:\\icons\\folder.gif")); final TreeItem child2a = new TreeItem(child2, SWT.NONE, 0); child2a.setText("2A"); final TreeItem child2b = new TreeItem(child2, SWT.NONE, 1); child2b.setText("2B"); final TreeItem child3 = new TreeItem(t, SWT.NONE, 2); child3.setText("3"); child3.setImage(new ...

Get SWT: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.