TreeSingleSelection.java Source code

Java tutorial

Introduction

Here is the source code for TreeSingleSelection.java

Source

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class TreeSingleSelection {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("TreeExample");

        Tree tree = new Tree(shell, SWT.SINGLE | SWT.BORDER);

        // Turn off drawing to avoid flicker
        tree.setRedraw(false);

        for (int i = 0; i < 5; i++) {
            TreeItem item = new TreeItem(tree, SWT.NONE);
            item.setText("Root Item " + i);

            for (int j = 0; j < 3; j++) {
                TreeItem child = new TreeItem(item, SWT.NONE);
                child.setText("Child Item " + i + " - " + j);

                for (int k = 0; k < 3; k++) {
                    TreeItem grandChild = new TreeItem(child, SWT.NONE);
                    grandChild.setText("Grandchild Item " + i + " - " + j + " - " + k);
                }
            }
        }
        tree.setRedraw(true);

        tree.setBounds(10, 10, 400, 400);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}