Skip to content

SWT Tutorial : Tree Example

package com.deveshcodelab.examples.swt;

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;

public class SWTSnippet16 extends Composite {
	public SWTSnippet16(Composite parent) {
		super(parent, SWT.NONE);
		setLayout(new FillLayout(SWT.VERTICAL));
		Tree tree = new Tree(this, SWT.BORDER | SWT.SINGLE| SWT.FULL_SELECTION);
		tree.setHeaderVisible(true);
		tree.setLinesVisible(true);
		TreeColumn column = new TreeColumn(tree, SWT.SINGLE| SWT.BORDER);
		column.setText("ITEM LIST");
		column.pack();
		for (int i = 1; i < 5; i++) {
			TreeItem treeItem = new TreeItem(tree, SWT.SINGLE);
			treeItem.setText("item "+i);
			
			for (int j = 0; j < i+1; j++) {
				TreeItem streeItem = new TreeItem(treeItem, SWT.SINGLE);
				streeItem.setText("sub item "+j);
			
			}
		}
	}

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout(1, false));

		Composite composite = new SWTSnippet16(shell);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *