原程序:Snippet114

 

1,Tree支持 SWT.CHECK 类型的树。

2,单击复选框或Item产生SWT.Selection事件,如果单击的是复选框,event.detail = SWT.CHECK。

3,双击Item产生SWT.DefaultSelection

 

public class Snippet114 {
	public static void main (String [] args) {
		Display display = new Display ();
		Shell shell = new Shell (display);
		shell.setLayout(new FillLayout());
		Tree tree = new Tree (shell, SWT.CHECK | SWT.BORDER);
		for (int i=0; i<12; i++) {
			TreeItem item = new TreeItem (tree, SWT.NONE);
			item.setText ("Item " + i);
		}
		tree.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				String string = event.detail == SWT.CHECK ? "Checked" : "Selected";
				System.out.println (event.item + " " + string);
			}
		});
		tree.addListener(SWT.DefaultSelection, new Listener() {
			public void handleEvent(Event event) {
				System.out.println ("DefaultSelection");
			}
		});
		shell.setSize (200, 200);
		shell.open ();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
	}
}