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*; import; 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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.